min max array search

JavaScript performance comparison

Revision 4 of this test case created

Info

http://ejohn.org/blog/fast-javascript-maxmin/

Preparation code

<script>
  var a = [48, 25, 98, 32, 20, 141, 36, 57, 12, 63, 78, 29, 57, 132];
</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 min/max apply()
Array.max = function(array) {
 return Math.max.apply(Math, array);
};

for (var i = 0; i < 10000; i++) {
 var max = Array.max(a);
}
pending…
linear lookup
var arraymax = function(array) {
 var l = array.length,
     max = array[0];

 while (--l) {
  if (array[l] > max) max = array[l];
 }

 return max;
}

for (var i = 0; i < 10000; i++) {
 var max = arraymax(a);
}
pending…
Math min/max apply() null
Array.max = function(array) {
 return Math.max.apply(null, array);
};

for (var i = 0; i < 10000; i++) {
 var max = Array.max(a);
}
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