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?