Symmetrical Array Difference (v2)

Revision 2 of this benchmark created on


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

Ready to run.

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

Revisions

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