Skip to main content
deleted 2194 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360

Extremely fast

Using this discovery I create extremely fast solution for medium size arrays and compare performance with chosen solutions (I name my solution as K). This solution modify input arrays

function K(arr1,arr2) {
  let r=[], h={};
  
  while(arr1.length) {
    let e = arr1.shift();
    if(!h[e]) h[e]=1 && r.push(e);
  }
  
  while(arr2.length) {
    let e = arr2.shift();
    if(!h[e]) h[e]=1 && r.push(e);
  }
  
  return r;
}


// TEST
var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];

console.log(K(array1,array2))

  • solution based on where-shift (K) is fastest (except small arrays on Safari whereH is fast) - for big arrays is >1000x faster than other solutions (!!!)/fastest
  • solution Fsolutions L is fast (fastest on Safari) for small arrays
  • solution D is fastest on chrome for big arrays
  • solution G is fast on small arrays
  • solution M is slowest for small arrays
  • solutions E,F,I are slowest for big arrays

We should note here that for very big arrays on Chrome the K solution can slow down - details here

enter image description hereenter image description here

  • for 2 elements arrays - you can run it HEREHERE
  • for 10000 elements arrays - you can run it HEREHERE

on solutions A, B, C, D, E, F, G, H, I, J, K (my), L, M presented in below snippet

// https://stackoverflow.com/a/10499519/860099
function A(arr1,arr2) {
  return _.union(arr1,arr2)
}

// https://stackoverflow.com/a/53149853/860099
function B(arr1,arr2) {
  return _.unionWith(arr1, arr2, _.isEqual);
}

// https://stackoverflow.com/a/27664971/860099
function C(arr1,arr2) {
  return [...new Set([...arr1,...arr2])]
}

// https://stackoverflow.com/a/48130841/860099
function D(arr1,arr2) {
  return Array.from(new Set(arr1.concat(arr2)))
}

// https://stackoverflow.com/a/23080662/860099
function E(arr1,arr2) {
  return arr1.concat(arr2.filter((item) => arr1.indexOf(item) < 0))
}

// https://stackoverflow.com/a/60421828/860099
function F(array1, array2){
  for (var i = 0; i < array2.length; i++)
    if (array1.indexOf(array2[i]) === -1)
      array1.push(array2[i]);
  return array1;
}

// https://stackoverflow.com/a/28631880/860099
function G(arr1,arr2) {
  var hash = {};
  var i;
  
  for (i = 0; i < arr1.length; i++) {
    hash[arr1[i]] = true;
  }
  for (i = 0; i < arr2.length; i++) {
    hash[arr2[i]] = true;
  }
  return Object.keys(hash);
}

// https://stackoverflow.com/a/13847481/860099
function H(a, b){
    var hash = {};
    var ret = [];

    for(var i=0; i < a.length; i++){
        var e = a[i];
        if (!hash[e]){
            hash[e] = true;
            ret.push(e);
        }
    }

    for(var i=0; i < b.length; i++){
        var e = b[i];
        if (!hash[e]){
            hash[e] = true;
            ret.push(e);
        }
    }

    return ret;
}


// https://stackoverflow.com/a/32108675/860099
function I(a1, a2) {
    var ai = [];
    for (var x = 0; x < a2.length; x++) {
        ai.push(x)
    };

    for (var x = 0; x < a1.length; x++) {
        for (var y = 0; y < ai.length; y++) {
            if (a1[x] === a2[ai[y]]) {
                ai.splice(y, 1);
                y--;
            }
        }
    }

    for (var x = 0; x < ai.length; x++) {
        a1.push(a2[ai[x]]);
    }

    return a1;
}

// https://stackoverflow.com/a/1584377/860099
function J(arr1,arr2) {
  function arrayUnique(array) {
      var a = array.concat();
      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);
          }
      }

      return a;
  }

  return arrayUnique(arr1.concat(arr2));
}

// my
function K(arr1,arr2) {
  let r=[], h={};
  
  while(arr1.length) {
    let e = arr1.shift();
    if(!h[e]) h[e]=1 && r.push(e);
  }
  
  while(arr2.length) {
    let e = arr2.shift();
    if(!h[e]) h[e]=1 && r.push(e);
  }
  
  return r;
}

// https://stackoverflow.com/a/25120770/860099
function L(array1, array2) {
    const array3 = array1.slice(0);
    let len1 = array1.length;
    let len2 = array2.length;
    const assoc = {};

    while (len1--) {
        assoc[array1[len1]] = null;
    }

    while (len2--) {
        let itm = array2[len2];

        if (assoc[itm] === undefined) { // Eliminate the indexOf call
            array3.push(itm);
            assoc[itm] = null;
        }
    }

    return array3;
}

// https://stackoverflow.com/a/39336712/860099
function M(arr1,arr2) {
  const comp = f => g => x => f(g(x));
  const apply = f => a => f(a);
  const flip = f => b => a => f(a) (b);
  const concat = xs => y => xs.concat(y);
  const afrom = apply(Array.from);
  const createSet = xs => new Set(xs);
  const filter = f => xs => xs.filter(apply(f));

  const dedupe = comp(afrom) (createSet);

  const union = xs => ys => {
    const zs = createSet(xs);  
    return concat(xs) (
      filter(x => zs.has(x)
       ? false
       : zs.add(x)
    ) (ys));
  }

  return union(dedupe(arr1)) (arr2)
}



// -------------
// TEST
// -------------

var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];

[A,B,C,D,E,F,G,H,I,J,K,L,M].forEach(f=> {
  console.log(`${f.name} [${f([...array1],[...array2])}]`);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>
  
This shippetsnippet only presents functions used in performance tests - it not perform tests itself!

enter image description hereenter image description here

UPDATE

I remove cases F,I,K because they modify input arrays and benchmark gives wrong results

Extremely fast

Using this discovery I create extremely fast solution for medium size arrays and compare performance with chosen solutions (I name my solution as K). This solution modify input arrays

function K(arr1,arr2) {
  let r=[], h={};
  
  while(arr1.length) {
    let e = arr1.shift();
    if(!h[e]) h[e]=1 && r.push(e);
  }
  
  while(arr2.length) {
    let e = arr2.shift();
    if(!h[e]) h[e]=1 && r.push(e);
  }
  
  return r;
}


// TEST
var array1 = ["Vijendra", "Singh"];
var array2 = ["Singh", "Shakya"];

console.log(K(array1,array2))

  • solution based on where-shift (K) is fastest (except small arrays on Safari where is fast) - for big arrays is >1000x faster than other solutions (!!!)
  • solution F is fast (fastest on Safari) for small arrays
  • solution M is slowest for small arrays
  • solutions E,F,I are slowest for big arrays

We should note here that for very big arrays on Chrome the K solution can slow down - details here

enter image description here

  • for 2 elements arrays - you can run it HERE
  • for 10000 elements arrays - you can run it HERE

on solutions A, B, C, D, E, F, G, H, I, J, K (my), L, M presented in below snippet

// https://stackoverflow.com/a/10499519/860099
function A(arr1,arr2) {
  return _.union(arr1,arr2)
}

// https://stackoverflow.com/a/53149853/860099
function B(arr1,arr2) {
  return _.unionWith(arr1, arr2, _.isEqual);
}

// https://stackoverflow.com/a/27664971/860099
function C(arr1,arr2) {
  return [...new Set([...arr1,...arr2])]
}

// https://stackoverflow.com/a/48130841/860099
function D(arr1,arr2) {
  return Array.from(new Set(arr1.concat(arr2)))
}

// https://stackoverflow.com/a/23080662/860099
function E(arr1,arr2) {
  return arr1.concat(arr2.filter((item) => arr1.indexOf(item) < 0))
}

// https://stackoverflow.com/a/60421828/860099
function F(array1, array2){
  for (var i = 0; i < array2.length; i++)
    if (array1.indexOf(array2[i]) === -1)
      array1.push(array2[i]);
  return array1;
}

// https://stackoverflow.com/a/28631880/860099
function G(arr1,arr2) {
  var hash = {};
  var i;
  
  for (i = 0; i < arr1.length; i++) {
    hash[arr1[i]] = true;
  }
  for (i = 0; i < arr2.length; i++) {
    hash[arr2[i]] = true;
  }
  return Object.keys(hash);
}

// https://stackoverflow.com/a/13847481/860099
function H(a, b){
    var hash = {};
    var ret = [];

    for(var i=0; i < a.length; i++){
        var e = a[i];
        if (!hash[e]){
            hash[e] = true;
            ret.push(e);
        }
    }

    for(var i=0; i < b.length; i++){
        var e = b[i];
        if (!hash[e]){
            hash[e] = true;
            ret.push(e);
        }
    }

    return ret;
}


// https://stackoverflow.com/a/32108675/860099
function I(a1, a2) {
    var ai = [];
    for (var x = 0; x < a2.length; x++) {
        ai.push(x)
    };

    for (var x = 0; x < a1.length; x++) {
        for (var y = 0; y < ai.length; y++) {
            if (a1[x] === a2[ai[y]]) {
                ai.splice(y, 1);
                y--;
            }
        }
    }

    for (var x = 0; x < ai.length; x++) {
        a1.push(a2[ai[x]]);
    }

    return a1;
}

// https://stackoverflow.com/a/1584377/860099
function J(arr1,arr2) {
  function arrayUnique(array) {
      var a = array.concat();
      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);
          }
      }

      return a;
  }

  return arrayUnique(arr1.concat(arr2));
}

// my
function K(arr1,arr2) {
  let r=[], h={};
  
  while(arr1.length) {
    let e = arr1.shift();
    if(!h[e]) h[e]=1 && r.push(e);
  }
  
  while(arr2.length) {
    let e = arr2.shift();
    if(!h[e]) h[e]=1 && r.push(e);
  }
  
  return r;
}

// https://stackoverflow.com/a/25120770/860099
function L(array1, array2) {
    const array3 = array1.slice(0);
    let len1 = array1.length;
    let len2 = array2.length;
    const assoc = {};

    while (len1--) {
        assoc[array1[len1]] = null;
    }

    while (len2--) {
        let itm = array2[len2];

        if (assoc[itm] === undefined) { // Eliminate the indexOf call
            array3.push(itm);
            assoc[itm] = null;
        }
    }

    return array3;
}

// https://stackoverflow.com/a/39336712/860099
function M(arr1,arr2) {
  const comp = f => g => x => f(g(x));
  const apply = f => a => f(a);
  const flip = f => b => a => f(a) (b);
  const concat = xs => y => xs.concat(y);
  const afrom = apply(Array.from);
  const createSet = xs => new Set(xs);
  const filter = f => xs => xs.filter(apply(f));

  const dedupe = comp(afrom) (createSet);

  const union = xs => ys => {
    const zs = createSet(xs);  
    return concat(xs) (
      filter(x => zs.has(x)
       ? false
       : zs.add(x)
    ) (ys));
  }

  return union(dedupe(arr1)) (arr2)
}



// -------------
// TEST
// -------------

var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];

[A,B,C,D,E,F,G,H,I,J,K,L,M].forEach(f=> {
  console.log(`${f.name} [${f([...array1],[...array2])}]`);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>
  
This shippet only presents functions used in performance tests - it not perform tests itself!

enter image description here

  • solution H is fast/fastest
  • solutions L is fast
  • solution D is fastest on chrome for big arrays
  • solution G is fast on small arrays
  • solution M is slowest for small arrays
  • solutions E are slowest for big arrays

enter image description here

  • for 2 elements arrays - you can run it HERE
  • for 10000 elements arrays - you can run it HERE

on solutions A, B, C, D, E, G, H, J, L, M presented in below snippet

// https://stackoverflow.com/a/10499519/860099
function A(arr1,arr2) {
  return _.union(arr1,arr2)
}

// https://stackoverflow.com/a/53149853/860099
function B(arr1,arr2) {
  return _.unionWith(arr1, arr2, _.isEqual);
}

// https://stackoverflow.com/a/27664971/860099
function C(arr1,arr2) {
  return [...new Set([...arr1,...arr2])]
}

// https://stackoverflow.com/a/48130841/860099
function D(arr1,arr2) {
  return Array.from(new Set(arr1.concat(arr2)))
}

// https://stackoverflow.com/a/23080662/860099
function E(arr1,arr2) {
  return arr1.concat(arr2.filter((item) => arr1.indexOf(item) < 0))
}


// https://stackoverflow.com/a/28631880/860099
function G(arr1,arr2) {
  var hash = {};
  var i;
  
  for (i = 0; i < arr1.length; i++) {
    hash[arr1[i]] = true;
  }
  for (i = 0; i < arr2.length; i++) {
    hash[arr2[i]] = true;
  }
  return Object.keys(hash);
}

// https://stackoverflow.com/a/13847481/860099
function H(a, b){
    var hash = {};
    var ret = [];

    for(var i=0; i < a.length; i++){
        var e = a[i];
        if (!hash[e]){
            hash[e] = true;
            ret.push(e);
        }
    }

    for(var i=0; i < b.length; i++){
        var e = b[i];
        if (!hash[e]){
            hash[e] = true;
            ret.push(e);
        }
    }

    return ret;
}



// https://stackoverflow.com/a/1584377/860099
function J(arr1,arr2) {
  function arrayUnique(array) {
      var a = array.concat();
      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);
          }
      }

      return a;
  }

  return arrayUnique(arr1.concat(arr2));
}


// https://stackoverflow.com/a/25120770/860099
function L(array1, array2) {
    const array3 = array1.slice(0);
    let len1 = array1.length;
    let len2 = array2.length;
    const assoc = {};

    while (len1--) {
        assoc[array1[len1]] = null;
    }

    while (len2--) {
        let itm = array2[len2];

        if (assoc[itm] === undefined) { // Eliminate the indexOf call
            array3.push(itm);
            assoc[itm] = null;
        }
    }

    return array3;
}

// https://stackoverflow.com/a/39336712/860099
function M(arr1,arr2) {
  const comp = f => g => x => f(g(x));
  const apply = f => a => f(a);
  const flip = f => b => a => f(a) (b);
  const concat = xs => y => xs.concat(y);
  const afrom = apply(Array.from);
  const createSet = xs => new Set(xs);
  const filter = f => xs => xs.filter(apply(f));

  const dedupe = comp(afrom) (createSet);

  const union = xs => ys => {
    const zs = createSet(xs);  
    return concat(xs) (
      filter(x => zs.has(x)
       ? false
       : zs.add(x)
    ) (ys));
  }

  return union(dedupe(arr1)) (arr2)
}



// -------------
// TEST
// -------------

var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];

[A,B,C,D,E,G,H,J,L,M].forEach(f=> {
  console.log(`${f.name} [${f([...array1],[...array2])}]`);
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"></script>
  
This snippet only presents functions used in performance tests - it not perform tests itself!

enter image description here

UPDATE

I remove cases F,I,K because they modify input arrays and benchmark gives wrong results

added 28 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360

Using this discovery I create extremely fast solution for medium size arrays and compare performance with chosen solutions (I name my solution as K). This solution modify input arrays

Using this discovery I create extremely fast solution for medium size arrays and compare performance with chosen solutions (I name my solution as K)

Using this discovery I create extremely fast solution for medium size arrays and compare performance with chosen solutions (I name my solution as K). This solution modify input arrays

edited body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360

Using this discovery I create extremely fast solution for bigmedium size arrays and perform test whichcompare performance with chosen solutions to show it (I name my solution as K)

Performance

Performance

enter image description here We should note here that for very big arrays on Chrome the K solution can slow down - details here

We should note here that for very big arrays on chrome the K solution can slow down - details hereenter image description here

And here are example resultstest run for chrome

enter image description hereenter image description here

Using this discovery I create extremely fast solution for big arrays and perform test which chosen solutions to show it (I name my solution as K)

Performance

enter image description here

We should note here that for very big arrays on chrome the K solution can slow down - details here

And here are example results for chrome

enter image description here

Using this discovery I create extremely fast solution for medium size arrays and compare performance with chosen solutions (I name my solution as K)

Performance

We should note here that for very big arrays on Chrome the K solution can slow down - details here

enter image description here

And here are example test run for chrome

enter image description here

added 28 characters in body
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading
Source Link
Kamil Kiełczewski
  • 90.1k
  • 32
  • 383
  • 360
Loading