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
Reduce
9.3M/s
Filter
9.1M/s
For loop
8.9M/s
Set
1.1M/s
View exact numbers
Test name
Executions per second
✓
Reduce
9,317,900 Ops/sec
Filter
9,128,153 Ops/sec
For loop
8,873,852 Ops/sec
Set
1,140,318 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; }, []);