fastest highest

JavaScript performance comparison

Test case created by Xotic750

Preparation code

<script src="http://underscorejs.org/underscore-min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var articles = [],
        howMany = 50000,
        i = 0,
        article, highest;
   
    while (i < howMany) {
      article = {};
      article.text = "foobar";
      article.color = "red";
      article.number = i;
      articles.push(article);
      i += 1;
    }
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
while
hownMany = articles.length;
i = 0;
while (i < howMany) {
  if (typeof highest !== "number" || articles[i].number > highest) {
    highest = i;
  }

  i += 1;
}
pending…
sort pop
articles.sort(function(a, b) {
  return (a.number - b.number);
});

highest = articles.pop();
pending…
jquery each
$.each(articles, function(key, value) {
  if (value.number > highest) {
    highest = value.number;
  }
});
pending…
slice sort
highest = articles.slice(0).sort(function(x, y) {
  return y.number - x.number
})[0]
pending…
underscore
highest = articles.indexOf(_.max(articles, function(obj) {
  return obj.number;
}));
pending…

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

Compare results of other browsers

0 comments

Add a comment