.

Tags:

After you’ve seen the chart of JavaScript keywords distribution, it’s time to go a bit deeper to the syntax level. This time let’s find out the most popular JavaScript statements. The specification lists 15 different types of statements, with the iteration statements having four possible variants. Like the previous attempt, using Esprima and its corpus of libraries (for the benchmark suite), I got the following chart:

For all intents and purposes, I also throw VariableDeclaration and FunctionDeclaration into the analysis. For the latter, the difference between a properly hoisted declaration and a declaration inside e.g. ForStatement is not taken into account, which is likely just fine.

Again, real-world applications can show a different chart. If you are interested in running the analysis on your own code, use the following quick tool statement.js (utilizing Esprima package):

var fs = require('fs'),
    esprima = require('esprima'),
    files = process.argv.splice(2);
 
files.forEach(function (filename) {
    var content = fs.readFileSync(filename, 'utf-8'),
        syntax = esprima.parse(content);
 
    JSON.stringify(syntax, function (key, value) {
        if (key === 'type') {
            if (value.match(/Declaration$/) ||
                value.match(/Statement$/)) {
                    console.log(value);
                }
        }
        return value;
    });
});

and run it with Node.js as follows:

node statement.js myapp.js mylib.js others/*.js | sort | uniq -c | sort -nr

How’s the statement distribution in your application?

  • Pingback: Dew Drop – April 19, 2012 (#1,309) | Alvin Ashcraft's Morning Dew

  • Starsix

    first bug:
    1. Javasript and another scripts are not a language it is a script.second bug:2. Java script should not be used to compensate incompatibility  and bugs of browsers  as is it usual .For what we should use scripts?Like in Flash, we should have a sophisticated framework and we should SCRIPT complete sentence of normal compilated code .!!!!!! If are some problems with to complicated javasript it should be changed framework not javascript . !!!!!! 

    • Peter Hanley

      You are confusing terms. Javascript is a ‘scripting language,’ which denotes a language that is compiled at runtime only. But a scripting language is a language, and they are useful, specifically with code that runs over networks where the processor type of the client computer can’t be specified before run time. 

    • Rob Grainger

      Just wrong on so many levels.

      1. JavaScript is a language. Hell, regular expressions are a language, albeit limited. JavaScript is Turing-complete, so capable of any computation – not necessarily effieciently.

      2. Adding exclamation marks does not make a point correct.

      3. “We should SCRIPT complete sentence of normal compilated code”. Sorry – that does not mean anything at all. Do you mean compiled? complicated?

      4. “JavaScript” should not be used to compensate incompatibility and bugs of browserw as it is usual. Ignoring the bad phrasing of the point, it is quite simply wrong. JavaScript IS widely used for exactly that purpose, and many excellent frameworks help abstract the differences between browsers.

  • Kay

    Interesting. Now I get to ask:

    Why is this useful? 

    Are we checking for cyclomatic complexity (and does that matter in javascript?), are we attempting to optimize, are we reaching a conclusion that more or less of x is better or worse? I think in order for us to make any use of this we need to understand how the various statements impact performance and memory in various browsers. I couldn’t begin to tell you if a switch in JS is efficient like a switch in C# (constant driven), or heavy like in Java.

    • http://ariya.ofilabs.com/ Ariya Hidayat

      Don’t read too much to it. This is just a for-fun exercise, far from being a useful scientific analysis :)