Skip to main content
Rescued links
Source Link
Michael M.
  • 10.8k
  • 11
  • 20
  • 40

https://web.archive.org/web/20200803234803/httpJSPerf://jsperf.com/merge-two-arrays-keeping-only-unique-values "Merge two arrays keeping only unique values" (archived)

A comment pointed out that one of my setup variables was causing my loop to pull ahead of the rest, because it didn't have to initialize an empty array to write to. I agree with that, so I've rewritten the test to even the playing field, and included an even faster option.

https://web.archive.org/web/20200803235454/httpJSPerf://jsperf.com/merge-two-arrays-keeping-only-unique-values/52 "Merge two arrays keeping only unique values" (archived)

https://web.archive.org/web/20200803234803/http://jsperf.com/merge-two-arrays-keeping-only-unique-values

A comment pointed out that one of my setup variables was causing my loop to pull ahead of the rest, because it didn't have to initialize an empty array to write to. I agree with that, so I've rewritten the test to even the playing field, and included an even faster option.

https://web.archive.org/web/20200803235454/http://jsperf.com/merge-two-arrays-keeping-only-unique-values/52

JSPerf: "Merge two arrays keeping only unique values" (archived)

A comment pointed out that one of my setup variables was causing my loop to pull ahead of the rest because it didn't have to initialize an empty array to write to. I agree with that, so I've rewritten the test to even the playing field, and included an even faster option.

JSPerf: "Merge two arrays keeping only unique values" (archived)

This change corrects the "uniqueifying" behavior by populating initial assoc hash with the keys from the first array. It doesn't _appear_ to hurt the speed too much. I've also updated it to not use a falsy check for a speed boost, and not use booleans for a little additional speed bump.
Source Link

http://jsperf.com/merge-two-arrays-keeping-only-unique-values/21http://jsperf.com/merge-two-arrays-keeping-only-unique-values/52

varlet whileLoopAlt = function (array1, array2) {
    varconst array3 = [];array1.slice(0);
    varlet arrlen1 = array1.concat(array2);length;
    varlet lenlen2 = arrarray2.length;
    varconst assoc = {};

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

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

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

    return array3;
};

http://jsperf.com/merge-two-arrays-keeping-only-unique-values/21

var whileLoopAlt = function(array1, array2) {
    var array3 = [];
    var arr = array1.concat(array2);
    var len = arr.length;
    var assoc = {};

    while(len--) {
        var itm = arr[len];

        if(!assoc[itm]) { // Eliminate the indexOf call
            array3.unshift(itm);
            assoc[itm] = true;
        }
    }

    return array3;
};

http://jsperf.com/merge-two-arrays-keeping-only-unique-values/52

let whileLoopAlt = function (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;
};
Active reading. Dressed the naked links.
Source Link
Peter Mortensen
  • 31.3k
  • 22
  • 109
  • 132
Loading
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
Loading
Removing last edit because code does not work properly. It does not satisfy the result of deduping the two arrays and combining them.
Source Link
slickplaid
  • 1.4k
  • 1
  • 17
  • 21
Loading
added one last test case
Source Link
slickplaid
  • 1.4k
  • 1
  • 17
  • 21
Loading
Added some qualifications to other answers. There are better answers in this list.
Source Link
slickplaid
  • 1.4k
  • 1
  • 17
  • 21
Loading
fixed issue with my original answer as one of the comments had pointed out. Added a second, faster result with the use of an associative array rather than indexOf on an basic array.
Source Link
slickplaid
  • 1.4k
  • 1
  • 17
  • 21
Loading
Missed defining some variables when copying it from my jsperf tests
Source Link
slickplaid
  • 1.4k
  • 1
  • 17
  • 21
Loading
added results
Source Link
slickplaid
  • 1.4k
  • 1
  • 17
  • 21
Loading
Source Link
slickplaid
  • 1.4k
  • 1
  • 17
  • 21
Loading