Appending to an array: push.apply vs. loop

JavaScript performance comparison

Test case created by T.J. Crowder

Preparation code

<script>
  var target;
  var source = ['n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
push.apply
target = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'];
target.push.apply(target, source);
if (target.length != 26) throw "Error";
pending…
for loop
var index;
target = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'];
for (index = 0; index < source.length; ++index) {
 target.push(source[index]);
}
if (target.length != 26) throw "Error";
pending…
safeguarded for..in loop
var name;
target = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm'];
for (name in source) {
 if (source.hasOwnProperty(name) && String(parseInt(name, 10)) === name) {
  target.push(source[name]);
 }
}
if (target.length != 26) throw "Error";
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.

0 Comments