Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Javascript: reduce VS for with Math.max
Compare the "reduce" method with "for" iteration and "for-of" iteration. We try to find the maximum value in a list of number. I also added Math.max with the spread operator.
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/136.0.0.0 Safari/537.36
Browser:
Chrome 136
Operating system:
Linux
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Number 1: use for iterator
11066683.0 Ops/sec
Number 2: use for-of iterator
27318910.0 Ops/sec
Number 3: use reduce
105021144.0 Ops/sec
Number 3: Math.max with spread
13797237.0 Ops/sec
Script Preparation code:
var list = [261,237,169,165,20,351,108,491,480,465,453,306,280,244,210,391,60,419,391,164,303,31,370,359,208,313,383,219,256,192,314,198,337,198,416,157,462,183,309,193,192,262,244,421,45,204,287,426,239,214];
Tests:
Number 1: use for iterator
let largest = -1; for (let i = 0; i < list.length; i++) { if (list[i] > largest) { largest = list[i]; } }
Number 2: use for-of iterator
let largest = -1; for (const item of list) { if (item > largest) { largest = item; } }
Number 3: use reduce
const largest = list.reduce((a,b) => Math.max(a, b));
Number 3: Math.max with spread
const largest = Math.max(...list);