Getting max value of an array

Benchmark created on


Setup

var a = [];
    for (var i = 0; i < 100000; i++)
    {
       a.push((Math.random() * 100000) | 0);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
For loop
var result = 0;
for (var c = 0; c < a.length; c++);
{
    if (a[c] > result) result = a[c];
}
 
ready
Math.max.apply
var result = Math.max.apply(Math, a);
 
ready
Array.reduce
var result = a.reduce(function(max, current){
    return current > max ? current : max;
}, 0);
 
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.