convert HTML collection into array

JavaScript performance comparison

Revision 4 of this test case created by Cong Liu

Preparation code

<div id="parent">
</div>
<script>
Benchmark.prototype.setup = function() {
    var parentNode = document.getElementById('parent');
    for (var i = 0, length = 100; i < length; i++) {
      var child = document.createElement('div');
      parentNode.appendChild(child);
    }
    var objs = parentNode.getElementsByTagName('div');
};
</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
HTML collection
for (var i = 0; i < objs.length; i++) {
  alert(objs[i].value);
}
pending…
convert HTML collection into Array
var objsArray = Array.prototype.slice.call(objs);
for (var i = 0; i < objsArray.length; i++) {
  alert(objsArray[i].value);
}
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