Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Javascript: reduce VS for
Compare the "reduce" method with the "for" iteration method. Use case: There is an array with different numbers, we will compare the performance between the two methods to find the quantity of numbers greater than zero in the array list.
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0
Browser:
Chrome 123
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
2 years ago
Number 2: using reduce method
23.1M/s
Number 1: use for iterator
1.2M/s
View exact numbers
Test name
Executions per second
✓
Number 2: using reduce method
23,131,388 Ops/sec
Number 1: use for iterator
1,190,803 Ops/sec
Script Preparation code:
var list = [1, 0, -2, -1, 3, 0, 1, 8, -10, 3, 0, -1, -3];
Tests:
Number 1: use for iterator
let countNumberGreaterThanZero = 0; for (let i = 0; i < list.length; i++) { if (list[i] > 0) { countNumberGreaterThanZero++; } }
Number 2: using reduce method
let countsNumbersGreaterThanZero = (acc, cur) => acc + (cur > 0 ? 1 : 0); list.reduce(countsNumbersGreaterThanZero, 0)