max

JavaScript performance comparison

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];
    bigGrades = [];
   
    for (var i = 1; i <= 1000; i++) {
     bigGrades.push(Math.random() * 10000 + 1)
    }
    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(bigGrades);
pending…
Sort
two(bigGrades);
pending…
While Loop
three(bigGrades);
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