# Symmetrical Array Difference (v2)

## Description

Compares to implementations of symmetricalArrayDiff

## Setup

``````function symmetricDifferenceQuadratic(a, b) {
const onlyInA = a.filter(item => !b.includes(item));
const onlyInB = b.filter(item => !a.includes(item));

return {
onlyInA,
onlyInB
};
}

function symmetricDifferenceLinear(a, b) {
const setA = new Set(a);
const setB = new Set(b);

const onlyInA = Array.from(setA).filter(item => !setB.has(item));
const onlyInB = Array.from(setB).filter(item => !setA.has(item));

return {
onlyInA,
onlyInB
};
}

function symmetricDifferenceLinearWithInBoth(a, b) {
const setA = new Set(a);
const setB = new Set(b);

const onlyInA = Array.from(setA).filter(item => !setB.has(item));
const onlyInB = Array.from(setB).filter(item => !setA.has(item));
const inBoth = Array.from(setA).filter(item => setB.has(item));

return {
onlyInA,
onlyInB,
inBoth
};
}

function generateTestData(size) {
// Generate numbers for the first array
const arrayA = Array.from({ length: size }, (_, index) => index);

// Generate numbers for the second array with some overlap.
// For simplicity, we start from size/2 to size + size/2 to create some overlapping and some unique numbers.
const arrayB = Array.from({ length: size }, (_, index) => index + Math.floor(size / 2));

return { arrayA, arrayB };
}

const testDataMin = generateTestData(10);
const testDataSmall = generateTestData(100);
const testDataMedium = generateTestData(1000);
const testDataLarge = generateTestData(100000);``````

## Test runner

Testing in
TestOps/sec
``const result1 = symmetricDifferenceQuadratic(testDataMin.arrayA, testDataMin.arrayB)``
linear min
``const result2 = symmetricDifferenceLinear(testDataMin.arrayA, testDataMin.arrayB)``
``const result3 = symmetricDifferenceQuadratic(testDataSmall.arrayA, testDataSmall.arrayB)``
linear small
``const result4 = symmetricDifferenceLinear(testDataSmall.arrayA, testDataSmall.arrayB)``
``const result5 = symmetricDifferenceQuadratic(testDataMedium.arrayA, testDataMedium.arrayB)``
linear medium
``const result6 = symmetricDifferenceLinear(testDataMedium.arrayA, testDataMedium.arrayB)``
``const result7 = symmetricDifferenceQuadratic(testDataLarge.arrayA, testDataLarge.arrayB)``
``const result8 = symmetricDifferenceLinear(testDataLarge.arrayA, testDataLarge.arrayB)``
``const result9 = symmetricDifferenceLinearWithInBoth(testDataLarge.arrayA, testDataLarge.arrayB)``