array concat arguments

JavaScript performance comparison

Revision 3 of this test case created by Emanuel Jackstare

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [1, 2, 3, 4, 5];
    var arg = (function() {
      return arguments;
    })({}, [1, 2, 3], 5, 1, [1, 2], "test");
   
   
    var slice = Array.prototype.slice;
    var concat = Array.prototype.concat;
    var push = Array.prototype.push;
};
</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
slice
[arr].concat(slice.call(arg));
pending…
concat
[arr].concat(concat.call(arg));
pending…
manual
var x = [arr];
var j = 1;
for (var i = 0, len = arg.length; i < len; i++) {
  x[j++] = arg[i++];
}
pending…
push
var x = [arr];
push.apply(x, arg);
pending…
manual 2
var x = [arr];
for (var i = 0, len = arg.length; i < len; i++) {
  x.push(arg[i++]);
}
pending…
manual 3
var x = [arr];
var j = 1;
var i = 0,
  len = arg.length;
x.length += len;
for (; i < len; i++) {
  x[j++] = arg[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