Iterating over collections of elements

JavaScript performance comparison

Test case created by Carlos Vializ

Preparation code

<script>function noop(o) { return o; }</script>
<div class='cv-test-container'></div>

      
<script>
Benchmark.prototype.setup = function() {
  var container = document.querySelector('.cv-test-container');
  for (let i = 0; i < 500; i++) {
    container.appendChild(document.createElement('div'));
    container.appendChild(document.createElement('form'));
  }

};

Benchmark.prototype.teardown = function() {
  var container = document.querySelector('.cv-test-container');
  container.innerHTML = '';

};
</script>

Preparation code output

<div class='cv-test-container'></div>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
simple for-loop undefined
var elements = document.querySelectorAll('form');
for (var i = 0; elements[i] != undefined; i++) {
  noop(elements[i]);
}
pending…
complex for-loop undefined
var elements = document.querySelectorAll('form');
for (var i = 0, element; (element = elements[i]) != undefined; i++) { 
  noop(element);
}
pending…
while
var elements = document.querySelectorAll('form');
var i = 0, element;
while ((element = elements[i++]) != undefined) { noop(element); }
pending…
length
var elements = document.querySelectorAll('form');
for (var i = 0; i < elements.length; i++) {
  noop(elements[i]);
}
pending…

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

0 Comments