Splice vs Push

JavaScript performance comparison

Test case created by Drew Wells


There is no easy way to replace the entire contents of an array in JavaScript. This compares the performance of splice to push.

Preparation code

  var a = [];
  a.length = 10000;
  var b = [];
  b.length = 10000;
  var c = [];
  c.length = 10000;
  var d = [];
  d.length = 10000;
  var aps = Array.prototype.splice;

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
//Replace a with c
aps.apply(a, c);
//Replace b with d
b.length = 0;
for (var i = 0, l = d.length; i < l; i++) {

Compare results of other browsers


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

John-David Dalton commented :

The big difference is you're modifying c directly and not doing the same for d. I created revision 2 to avoid modifying the source arrays directly.

Comment form temporarily disabled.

Add a comment