convert HTML collection into array

JavaScript performance comparison

Revision 7 of this test case created

Info

Some of the previous revisions were incrementing the amount of children in parentdiv after each loop, resulting in slower code the longer the benchmark ran.

This revision does not have that issue.

Preparation code

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