min max array search

JavaScript performance comparison

Revision 5 of this test case created by Kevin

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…
Insert sort pop
function insertSort(arr) {
  for (var i = 0, j, tmp; i < arr.length; ++i) {
    tmp = arr[i];
    for (j = i - 1; j >= 0 && arr[j] > tmp; --j) {
      arr[j + 1] = arr[j];
    }
    arr[j + 1] = tmp;
  }
  return arr;
}

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