Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
`filter().length` vs. `reduce` vs. `for`
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 (Windows NT 10.0; Win64; x64; rv:129.0) Gecko/20100101 Firefox/129.0
Browser:
Firefox 129
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
filter().length
163259.3 Ops/sec
reduce
164150.5 Ops/sec
for loop
164275.4 Ops/sec
Script Preparation code:
var sample = Array(1000).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) => total + (sample.includes(id) ? 1 : 0), 0);
for loop
var total = 0; for (const id of selected) { if (sample.includes(id)) { total++; } }