Performance impact of referencing arguments[]

Benchmark created on


Description

Testing the performance impact of simply referencing the implicit arguments array in a function. Loops are included in the function to minimize the impact of indexing the array and emphasize the impact of simply referring to it.

Preparation HTML

<script>
  function assert(condition) {
    if (!condition) throw new Error("Assertion failed!");
  }
  
  function f1(a, b, c) {
    var d = 0;
    for (var i = 0; i < c; i++) {
       d += (a + b);
    }
    return d;
  }
  
  function f2() {
    var a = arguments[0], b = arguments[1], c = arguments[2], d = 0;
    for (var i = 0; i < c; i++) {
       d += (a + b);
    }
    return d;
  }
  
  function f3(args) {
    var a = args[0], b = args[1], c = args[2], d = 0;
    for (var i = 0; i < c; i++) {
       d += (a + b);
    }
    return d;
  }
  
  function f4(a, b, c) {
    var d = arguments.length;
    for (var i = 0; i < c; i++) {
       d += (a + b);
    }
    return d;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
standard argument list
assert(f1(1, 2, 1000) === 3000);
ready
arguments[]
assert(f2(1, 2, 1000) === 3000);
ready
arguments passed as explicit array
assert(f3([1, 2, 1000]) === 3000);
ready
only referencing arguments.length
assert(f4(1, 2, 1000) === 3003);
ready

Revisions

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