.

Tags:

Most popular keywords? The quick short answer is: it depends.

While investigating the proposal for a faster isKeyword() function in Esprima, I decided to see the frequency of JavaScript keywords appearing in most common libraries. Esprima already has a selection of popular libraries as part of its comprehensive benchmark suite. When I eliminated the duplicated versions (so that each library is represented only once), the distribution of the keywords is depicted in the following chart. The dominant ones are this, function, if, return, and var, followed by a long tail of other keywords.

There are several things which are validated by the above data. For example, it is expected that finally should occur less, or at least the same, as try. Due to then nature of the construct, case typically shows up more often than its parent switch. Also it will be weird if else is found more than if, same case with do and while pair.

Obviously the above beautiful color chart is only for keywords in libraries and frameworks. For real-world user applications, the situation could be different. If you are interested in running the analysis on your own code, use the following quick tool keyword.js:

var fs = require('fs'),
    esprima = require('esprima'),
    files = process.argv.splice(2);
 
files.forEach(function (filename) {
    var content = fs.readFileSync(filename, 'utf-8'),
        tokens = esprima.parse(content, { tokens: true }).tokens;
 
    tokens.forEach(function (token) {
        if (token.type === 'Keyword') {
            console.log(token.value);
        }
    });
});

and run it with Node.js:

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

or alternatively, to go recursive in a certain directory:

find /path/to/dir -type f -name '*.js' -exec node keyword.js '{}' + |
  sort | uniq -c |  sort -nr

What do you get for your project?

  • http://twitter.com/thomasfuchs Thomas Fuchs

    Thanks for compiling this. 

    As an interesting aside, CoffeeScript eliminates two our of the first five completely (`return` and `var`), and has much shorter ways to express `function` (->) and `this` (@).

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

      Two of them, this and var, potentially trip JavaScript n00b.

  • http://twitter.com/thomasfuchs Thomas Fuchs

    Thanks for compiling this. 

    As an interesting aside, CoffeeScript eliminates two our of the first five completely (`return` and `var`), and has much shorter ways to express `function` (->) and `this` (@).

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

      Two of them, this and var, potentially trip JavaScript n00b.

  • Bertjan

    From a small project I’m currently working on:

         98 return     87 var     87 function     56 case     38 if     16 break     15 throw     11 this       8 else       7 switch       7 for       7 default       6 typeof       4 try       4 catch       3 in       1 instanceof

  • Bertjan

    From a small project I’m currently working on:

         98 return     87 var     87 function     56 case     38 if     16 break     15 throw     11 this       8 else       7 switch       7 for       7 default       6 typeof       4 try       4 catch       3 in       1 instanceof

  • http://www.senchatouchbits.com/ Steffen Hiller

    Sencha Touch 2 project. No ‘new’ because of Ext.create and the general nature of ST.

     302 this
     141 function
      84 var
      77 if
      17 return
      15 else

  • http://www.senchatouchbits.com/ Steffen Hiller

    Sencha Touch 2 project. No ‘new’ because of Ext.create and the general nature of ST.

     302 this
     141 function
      84 var
      77 if
      17 return
      15 else

  • Enrique Quintano

    hey Ariya! nice post!!!

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

      Thank you!

  • Enrique Quintano

    hey Ariya! nice post!!!

  • http://twitter.com/tessekkur tessekkur

    nice post!

    and nice discussion! ;)

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

      Thanks!

  • http://twitter.com/tessekkur tessekkur

    nice post!

    and nice discussion! ;)

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

      Thanks!

  • http://profiles.google.com/uuf6429 Christian Sciberras

    Wonder what the usage count of eval is :)

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

      Fortunately it’s not a keyword!

  • http://profiles.google.com/uuf6429 Christian Sciberras

    Wonder what the usage count of eval is :)

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

      Fortunately it’s not a keyword!

  • http://twitter.com/MatjazL Matjaž Lipuš

      5426 this
       3119 if
       3119 function
       2835 return
       2192 var
        792 else
        493 for
        365 typeof
        324 new
        268 in
        236 case
        195 break
         76 while
         71 void
         52 delete
         44 throw
         32 try
         32 catch
         30 switch
         24 continue
         18 instanceof
         15 default
          3 do
          2 finally

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

      The same amount of ‘if’ and ‘function’ is very interesting!

  • http://twitter.com/MatjazL Matjaž Lipuš

      5426 this
       3119 if
       3119 function
       2835 return
       2192 var
        792 else
        493 for
        365 typeof
        324 new
        268 in
        236 case
        195 break
         76 while
         71 void
         52 delete
         44 throw
         32 try
         32 catch
         30 switch
         24 continue
         18 instanceof
         15 default
          3 do
          2 finally

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

      The same amount of ‘if’ and ‘function’ is very interesting!

  • GlitchMr

    Very small project :).
    32 return
    27 if
    19 case
    12 function
    10 else
    8 var
    6 this
    6 new
    3 for
    2 void
    2 typeof
    2 throw
    2 break
    1 switch
    1 instanceof
    1 in
    1 default

  • Pingback: In the News: 2012-03-13 | Klaus' Korner()