forEach() compared to for() and while()

JavaScript performance comparison

Revision 9 of this test case created

Info

Comparison of different methods of looping through nodelists

Preparation code

<div>
</div>
<script>
  var nodes = document.getElementsByTagName("div"),
      _forEach = Array.prototype.forEach,
      x;
</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
forEach
[].forEach.call(nodes, function(node) {
  x = node;
});
pending…
for
for (var i = 0, length = nodes.length; i < length; i++) {
  x = nodes[i];
}
pending…
for (backwards)
for (var i = nodes.length; i--;) {
  x = nodes[i];
}
pending…
while
var i = -1,
    length = nodes.length;
while (++i < length) {
  x = nodes[i];
}
pending…
while (backwards)
var i = nodes.length;
while (i--) {
  x = nodes[i];
}
pending…
forEach (Cached)
_forEach.call(nodes, function(node) {
  x = node;
});
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