arguments to array

JavaScript performance comparison

Revision 4 of this test case created

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

  var f5 = (function() {
    var slice = [].slice;
    return function() {
      return slice.call(arguments);
    };
  })();

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

  var f7 = function() {
      var i = arguments.length,
          args = new Array(i);
      while (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…
f5
f5(1, 2, 3, 4, 5);
pending…
f6
f6(1, 2, 3, 4, 5);
pending…
f7
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