Tests:
  • Filter

    x
     
    const array1 = [1, 2, 3, 4, 5];
    const array2 = [3, 4, 5, 6, 7];
    const difference =
        array1.filter((element) => !array2.includes(element));
  • For loop

     
    const array1 = [1, 2, 3, 4, 5];
    const array2 = [3, 4, 5, 6, 7];
    const difference = [];
     
        for (let i = 0; i < array1.length; i++) {
            if (array2.indexOf(array1[i]) === -1) {
                difference.push(array1[i]);
            }
        }
  • Set

     
    const array1 = [1, 2, 3, 4, 5];
    const array2 = [3, 4, 5, 6, 7];
    const set1 = new Set(array1);
    const set2 = new Set(array2);
     
    const difference = [...set1].filter(
        (element) => !set2.has(element));
  • Reduce

     
    const array1 = [1, 2, 3, 4, 5];
    const array2 = [3, 4, 5, 6, 7];
    const difference = array1.reduce((result, element) => {
        if (array2.indexOf(element) === -1) {
            result.push(element);
        }
        return result;
    }, []);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    Filter
    For loop
    Set
    Reduce

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Test name Executions per second
Filter 5361588.5 Ops/sec
For loop 35094612.0 Ops/sec
Set 2284013.8 Ops/sec
Reduce 14183769.0 Ops/sec