Nested Fors vs. Iterative fors

JavaScript performance comparison

Test case created by Ian Bentley

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="https://raw.github.com/documentcloud/underscore/master/underscore-min.js"></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
one hundred iterations with ten function calls each
function doSomething(a) {
  a * 10 + a * 1000000.0 / 50.0;
}

_.each(_.range(100), function(a) {
  doSomething(a);
  doSomething(a);
  doSomething(a);
  doSomething(a);
  doSomething(a);
  doSomething(a);
  doSomething(a);
  doSomething(a);
  doSomething(a);
  doSomething(a);
});
pending…
one hundred iterations ten times with one call each
function doSomething(a) {
  a * 10 + a * 1000000.0 / 50.0;
}
var range = _.range(100);
_.each(range, function(a) {
  doSomething(a);
});
_.each(range, function(a) {
  doSomething(a);
});
_.each(range, function(a) {
  doSomething(a);
});
_.each(range, function(a) {
  doSomething(a);
});
_.each(range, function(a) {
  doSomething(a);
});
_.each(range, function(a) {
  doSomething(a);
});
_.each(range, function(a) {
  doSomething(a);
});
_.each(range, function(a) {
  doSomething(a);
});
_.each(range, function(a) {
  doSomething(a);
});
_.each(range, function(a) {
  doSomething(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