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

JavaScript performance comparison

Revision 10 of this test case created

Info

Comparison of different methods of looping through nodelists

Preparation code

<div></div>
<script>
  var nodes = document.getElementsByTagName("DIV");
  var 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(item) {
 x = item;
});
pending…
for
var nodesLength = nodes.length;
for (var i = 0; i < nodesLength; i++) {
 x = nodes[i];
}
pending…
while
var i = 0;
var nodesLength = nodes.length;

while (i < nodesLength) {
 x = nodes[i];
 i++;
}
pending…
while() backwards
var nodesLength = nodes.length;
while (nodesLength--) {
 x = nodes[nodesLength];
}
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