List function arguments

JavaScript performance comparison

Test case created by naholyr

Info

Function helper returning the list of the parameter names expected by the function.

See https://gist.github.com/1865010

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    /** Tested functions **/
   
   
    function foo1 (a, b, c) {
      return a + b + c;
    }
   
    function foo2_new_lines
    ( a
    , b,
    c) { return a+b+c
    }
   
    function foo3_no_space(a,b,c){return a+b+c}
   
    var foo4_anonymous = function (a, b, c) { return a + b + c };
   
    var foo5_anonymous_named = function foo5 (a, b, c) { return a+b+c };
   
   
   
   
    /** Prototype extension **/
   
   
    Function.prototype.argNamesES6 = function () {
      var s = this.toString();
      return s.substring(s.indexOf('(')+1, s.indexOf(')')).replace(/[\r\n\s]*/g, '').replace(/\\+['"]/g, '').replace(/=\s*(["']).*?\1/g, '').replace(/=.*?(,|$)/g, '').split(',');
    };
   
    Function.prototype.argNamesES5 = function () {
      var s = this.toString();
      return s.substring(s.indexOf('(')+1, s.indexOf(')')).replace(/[\r\n\s]*/g, '').split(',');
    };
   
   
   
    /** Do the tests **/
   
    function checkFoo (foo, method) {
      if (!foo[method]) throw "Method not found";
      var args = foo[method]();
      if (args.length != 3) throw "Invalid length";
      if (args[0] != 'a' || args[1] != 'b' || args[2] != 'c') throw "Invalid names";
    }
   
    function doTest (version) {
      var method = 'argNames' + version;
      checkFoo(foo1, method);
      checkFoo(foo2_new_lines, method);
      checkFoo(foo3_no_space, method);
      checkFoo(foo4_anonymous, method);
      checkFoo(foo5_anonymous_named, method);
    }
   
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Normal version
doTest('ES5');
pending…
ES6-ready version
doTest('ES6');
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment