HTML Preparation code:
AخA
 
1
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
x
 
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min;
}
const thousand = 1000;
var max = 10 * thousand;
var arr1 = [];
var arr2 = [];
for (var i = 0; i <= max; i++) {
    arr1.push(getRandomInt(0, max));
    arr2.push(getRandomInt(0, max));
}
console.log(arr1);
console.log(arr2);
Tests:
  • Double loop

     
    function getArrayDiffs2(array = [], comparison = []) {
        const result = [];
        // ignore duplicates and look for matches
        new Set(array).forEach(a => {
            const match = comparison.find(b => _.isEqual(a, b));
            if (match) {
                result.push(["UNCHANGED", a]);
            }
            else {
                result.push(["NEW", a]);
            }
        });
        // ignore duplicates and look for matches
        new Set(comparison).forEach(a => {
            const match = array.find(b => _.isEqual(a, b));
            if (!match) {
                result.push(["DELETED", a]);
            }
        });
        return result;
    }
    getArrayDiffs2(arr1,arr2);
  • Single loop

     
    function getArrayDiffs1(array = [], comparison = []) {
        const result = [];
        const comparisonSet = new Set(comparison);
        const cArray = Array.from(comparisonSet);
        new Set(array).forEach(a => {
            const match = cArray.indexOf(b => _.isEqual(a, b));
            if (match > 0) {
                result.push(["UNCHANGED", a]);
                cArray.splice(match, 1);
            }
            else {
                result.push(["NEW", a]);
            }
        });
        // ignore duplicates and look for matches
        cArray.forEach(a => {
          result.push(["DELETED", a]);
        });
        return result;
    }
    getArrayDiffs1(arr1,arr2);
  • XOR

     
    function getArrayDiffs3(array = [], comparison = []) {
      const UNCHANGED = _.intersectionWith(array, comparison, _.isEqual);
      const newArray = _.differenceWith(array,UNCHANGED, _.isEqual);
      const DELETED = _.differenceWith(comparison,UNCHANGED, _.isEqual);
      const result = [];
      UNCHANGED.forEach(a => {
        result.push(["UNCHANGED", a]);
      });
      DELETED.forEach(a => {
        result.push(["DELETED", a]);
      });
      newArray.forEach(a => {
        result.push(["NEW", a]);
      });
      return result;
    }
    getArrayDiffs3(arr1,arr2);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Double loop
    Single loop
    XOR

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 3 years ago)
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36
Chrome 91 on Linux
View result in a separate tab
Test name Executions per second
Double loop 0.1 Ops/sec
Single loop 55.7 Ops/sec
XOR 9.5 Ops/sec