concat-array (v3)

Revision 3 of this benchmark created by MoonScript on


Description

Different methods of concatenating two arrays with 500 numbers. The first test uses the simple concat method, which creates a new array; the rest operate on the existing array.

Removed setup function because 'combinedArr is not defined' error in IE8 made me suspicious of the variable scope of the setup function and the test function. Now each test function will define its own instance of 'combinedArr'.

Preparation HTML

<script>
  var mainArr = [];
  for (var i=0;i<500;i++) {
    mainArr.push(Math.random());
  }
  var arrToAppend = mainArr.slice(0);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
concat new
var combinedArr = mainArr.slice(0);
combinedArr = combinedArr.concat(arrToAppend);
ready
push existing
var combinedArr = mainArr.slice(0);
Array.prototype.push.apply(combinedArr, arrToAppend);
ready
unshift existing
var combinedArr = mainArr.slice(0);
Array.prototype.unshift.apply(combinedArr, arrToAppend);
ready
splice existing
var combinedArr = mainArr.slice(0);
Array.prototype.splice.apply(combinedArr, [combinedArr.length, 0].concat(arrToAppend));
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.