Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
`filter().length` vs. `reduce` 2
Compare `filter().length` vs `reduce` vs `for` loop for counting array elements that are present in another array.
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser:
Chrome 135
Operating system:
Linux
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
filter().length
633012.5 Ops/sec
reduce
648380.8 Ops/sec
for loop
644809.5 Ops/sec
Script Preparation code:
var sample = Array(10000).fill(0).map((_, idx) => idx); var selected = [10, 134, 245, 355, 489, 583, 682, 823, 2081, 3892, 4892, 5829, 6832];
Tests:
filter().length
var total = selected.filter(id => sample.includes(id)).length;
reduce
var total = selected.reduce((total, id) => sample.includes(id) ? total + 1 : total, 0);
for loop
var total = 0; for (const id of selected) { if (sample.includes(id)) { total++; } }