arguments to array

JavaScript performance comparison

Revision 2 of this test case created by fengmk2

Preparation code

<script>
var f1 = function () {
  return [].slice.call(arguments);
};

var f2 = function () {
  return Array.prototype.slice.call(arguments);
};

var f3 = function () {
  var args = [];
  for (var i = 0, l = arguments.length; i < l; i++) {
    args.push(arguments[i]);
  }
  return args;
};

var f4 = function () {
  var args = [];
  for (var i = 0, l = arguments.length; i < l; i++) {
    args[i] = arguments[i];
  }
  return args;
};
</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
f1
f1(1, 2, 3, 4, 5);
pending…
f2
f2(1, 2, 3, 4, 5);
pending…
f3
f3(1, 2, 3, 4, 5);
pending…
f4
f4(1, 2, 3, 4, 5);
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:

1 comment

fengmk2 (revision owner) commented :

Google JavaScript Style Guide say:

"You can also use an array as a stringbuilder, and convert it into a string with myArray.join(''). Note that since assigning values to an array is faster than using push() you should use assignment where possible."

That is wrong in V8.

Comment form temporarily disabled.

Add a comment