max

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    grades = [6, 1, 35, 135, 2, 52];
    grades2 = [2, 54, 3535, 34, 34, 5, 1, 6, 23, 36, 1, 5, 2, 6, 1, 5, 1535];
   
    function one(grades) {
     return Math.max.apply(null, grades);
    }
   
    function two(grades) {
     return grades.sort(function(a, b) { return b - a } )[0];
    }
   
    function three(grades) {
     // assuming grades is not null/empty
     var max = grades[0], i = 1, l = grades.length;
     while (i < l) {
      var v = grades[i++];
      if (v > max) max = v;
     }
     return max;
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Math
one(grades2);
 
pending…
Sort
two(grades2);
pending…
own max
three(grades2);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment