Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Array Diffs
Filter vs For loop vs Set vs Reduce
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser:
Chrome 123
Operating system:
Windows
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
Filter
9128153.0 Ops/sec
For loop
8873852.0 Ops/sec
Set
1140317.9 Ops/sec
Reduce
9317900.0 Ops/sec
Tests:
Filter
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; }, []);