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

JavaScript performance comparison

Revision 15 of this test case created

Info

Comparison of different methods of looping through nodelists

Preparation code

<div class="building"></div>
<div class="building"></div>
<div class="building"></div>
<div class="building"></div>
<div class="building"></div>
<div class="building"></div>
<script>
Benchmark.prototype.setup = function() {
    var count = 0;
};
</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
with var count
count = document.querySelectorAll('.building');
for (var i = 0; i < count.length; i++) {
  count[i].addEventListener('click', function(event) {
    alert("hola");
  }, false);
}
pending…
without var count
                        for (var i = 0; i < document.querySelectorAll('.building').length; i++) {
                          count[i].addEventListener('click', function(event) {
                            alert("hola");
                          }, false);
                        }
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