merge-array implementations

Benchmark created by shiling on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Setup

var arr1 = Array.from(Array(50000), (x,i) => i + 1)
  var arr2 = Array.from(Array(50000), (x,i) => i - 1)
  
  var arr1Length = arr1.length; 
  var arr2Length = arr2.length;

Test runner

Ready to run.

Testing in
TestOps/sec
concat
arr1 = arr1.concat(arr2)
ready
concat (naive 1)
// Create result array
var arr3 = []

// Add Array 1
for(var i = 0; i < arr1Length; i++){
  arr3[i] = arr1[i]
}

// Add Array 2
for(var i = 0; i < arr2Length; i++){
  arr3[arr1Length + i] = arr2[i]
}
ready
concat (naive 2)
// Create result array with preallocated size
var arr3 = Array(arr1Length + arr2Length)

// Add Array 1
for(var i = 0; i < arr1Length; i++){
  arr3[i] = arr1[i]
}

// Add Array 2
for(var i = 0; i < arr2Length; i++){
  arr3[arr1Length + i] = arr2[i]
}
ready
_.concat
arr1 = _.concat(arr1, arr2)
ready
push
Array.prototype.push.apply(arr1, arr2)
ready
push (naive 1)
for(var i = 0; i < arr2Length; i++){
  arr1[arr1Length + i] = arr2[i]
}
ready
push (naive 2)
// Pre allocate size
arr1.length = arr1Length + arr2Length

// Add arr2 items to arr1
for(var i = 0; i < arr2Length; i++){
  arr1[arr1Length + i] = arr2[i]
}
ready
push (naive 3)
for(var i = 0; i < arr2Length; i++){
  arr1.push(arr2[i])
}
ready

Revisions

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