Min and max in array

JavaScript performance comparison

Revision 3 of this test case created by Linus Unnebäck

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var values = [41,73,98,62,85,51,28,89,20];
  
  var minReduce = function (arr) {
    return arr.reduce(function (p, v) {
      return Number( p < Number(v) ? p : v );
    });
  };
  var maxReduce = function (arr) {
    return arr.reduce(function (p, v) {
      return Number( p > Number(v) ? p : v );
    });
  };
  
  var minMath = function (arr) {
    return Math.min.apply(Math, arr);
  };
  var maxMath = function (arr) {
    return Math.max.apply(Math, arr);
  };
  
  var minLoop = function (arr) {
    var len = arr.length, min = Infinity;
    while (len--) {
      if (Number(arr[len]) < min) {
        min = Number(arr[len]);
      }
    }
    return min;
  };
  var maxLoop = function (arr) {
    var len = arr.length, max = -Infinity;
    while (len--) {
      if (Number(arr[len]) > max) {
        max = Number(arr[len]);
      }
    }
    return max;
  };

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Reduce
maxReduce(values);
minReduce(values);
pending…
Math
maxMath(values);
minMath(values);
pending…
Loop
maxLoop(values);
minLoop(values);
pending…

Compare results of other browsers

Revisions

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

0 Comments