Merge 2 arrays without duplicate

JavaScript performance comparison

Test case created by Pitouli

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  let array1 = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",  "q", "u", "y"]
  
  let array2 = ["b", "f", "j", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
  
  let array3 = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "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
Set and Array Destructuring
var a = [...new Set([...array1 ,...array2])];
pending…
Concat and Splice
var a = array1.concat(array2);
for (var i = 0; i < a.length; ++i) {
    for (var j = i + 1; j < a.length; ++j) {
        if (a[i] === a[j])
            a.splice(j--, 1);
    }
}
pending…
forEach and Includes
var a = array1.slice();
array2.forEach(v => { a.includes(v) ? null : a.push(v) })
pending…
While Unshift
var arr = array1.concat(array2),
  len = arr.length,
  a = [];

while (len--) {
  var itm = arr[len];
  if (a.indexOf(itm) === -1) {
    a.unshift(itm);
  }
}
pending…
Concat and Filter
var a = array1.concat(array2.filter(function(i) {
  return array1.indexOf(i) == -1;
}));
pending…
forEach and includes (MODIFY array1)
array2.forEach(v => array1.includes(v) ? null : array1.push(v));
pending…
For IndexOf Push
for (var i = 0; i < array2.length; i++)
    if (array1.indexOf(array2[i]) === -1)
      array1.push(array2[i]);
pending…

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

0 Comments