Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Speed of finding max value
(version: 1)
Uses 4 different approaches: loop, Math.max.apply, reduce, and sort
Comparing performance of:
Loop vs Math.max.apply vs Reduce vs Sort
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const toSearch = []; for (let index = 0; index < 5000; index++) { toSearch.push({ x: Math.random(), y: Math.random() }); } toSearch.push({ x: 1, y: 1 });
Tests:
Loop
let max = 0; for (let i = 0; i < toSearch.length; i++) { if (toSearch[i].x > max) { max = toSearch[i].x; } }
Math.max.apply
let max = Math.max.apply(null, toSearch.map(function (o) { return o.x; }));
Reduce
let max = toSearch.reduce((acc, value) => { return (acc = acc > value.x ? acc : value.x); }, 0);
Sort
toSearch.sort((one, two) => two.x - one.x); let max = toSearch[0].x;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Loop
Math.max.apply
Reduce
Sort
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Loop
39319.7 Ops/sec
Math.max.apply
22121.8 Ops/sec
Reduce
83916.8 Ops/sec
Sort
12199.4 Ops/sec
Related benchmarks:
Math max
Math.max() vs Array.reduce() vs for element of array loop
Find Max
array math.max vs for loop
Math.max() vs Array.reduce() vs For-loop
for vs reduce (find max value)
for vs reduce (find max value) i10000
FindLargestProduct
Math.max() vs Function with For loop
Comments
Confirm delete:
Do you really want to delete benchmark?