cost of arguments.length

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var ar = [];
    for (var i = 0; i < 1000; i++) {
      ar.push(i);
    }
   
    function error() {
    throw 'error';
    }
};
</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
plain
function test(a) {
  return a * 2 + 1;
}

var sum = 0;
for (var i = 0; i < ar.length; i++) {
  sum += test(ar[i]);
}
pending…
with arguments
function test(a) {
  return (arguments.length >= 1) ? (a * 2 + 1) : 0;
}

var sum = 0;
for (var i = 0; i < ar.length; i++) {
  sum += test(ar[i]);
}
pending…
with checks
function test(a, b) {
  return (a != null) ? (a * 2 + 1) : 0;
}

var sum = 0;
for (var i = 0; i < ar.length; i++) {
  sum += test(ar[i]);
}
pending…
with value check
function test(a) {
  return (a * 2 + 1) | 0;
}

var sum = 0;
for (var i = 0; i < ar.length; i++) {
  sum += test(ar[i]);
}
pending…
use argunents
function test() {
  return (arguments[0] * 2 + 1) | 0;
}

var sum = 0;
for (var i = 0; i < ar.length; i++) {
  sum += test(ar[i]);
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment