arguments to array

JavaScript performance comparison

Revision 16 of this test case created

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<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;
};

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

var f6 = function () {
  return $.makeArray(arguments).sort();
};

var f7 = function (array) {
  var args = new Array(array.length);
  for (var i = 0, l = array.length; i < l; i++) {
    args[i] = array[i];
  }
  return args;
};
</script>

Preparation code output

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…
f5
f5(1, 2, 3, 4, 5);
pending…
f6
f6(1, 2, 3, 4, 5);
pending…
f7([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:

0 comments

Add a comment