cost of arguments.length

JavaScript performance comparison

Revision 4 of this test case created

Preparation code

<script>
var ar = [];
for (var i = 0; i < 1000; i++) {
  ar.push(i);
}

function error() {
  throw 'error';
}

function test1(a) {
  return a * 2 + 1;
}

function test2(a) {
  return (arguments.length >= 1) ? (a * 2 + 1) : 0;
}

function test3(a, b) {
  return (a != null) ? (a * 2 + 1) : 0;
}

function test4(a) {
  return (a * 2 + 1) | 0;
}
</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
var sum = 0;
for (var i = 0; i < ar.length; i++) {
  sum += test1(ar[i]);
}
pending…
with arguments
var sum = 0;
for (var i = 0; i < ar.length; i++) {
  sum += test2(ar[i]);
}
pending…
with checks
var sum = 0;
for (var i = 0; i < ar.length; i++) {
  sum += test3(ar[i]);
}
pending…
with value check
var sum = 0;
for (var i = 0; i < ar.length; i++) {
  sum += test4(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