convert HTML collection into array

JavaScript performance comparison

Revision 7 of this test case created


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">
<script type=text/js>
var parentNode = document.getElementById('parent'), i;
for (length = 100, i = length; i > length; i--) {
Benchmark.prototype.setup = function() {
    var parentNode = document.getElementById('parent'), objs = parentNode.getElementsByTagName('div');

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];
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];
convert HTML collection into Array with Array.slice
var objsArray =, _len = objsArray.length, i, obj;
for (i = 0; i < _len; i++) {
  obj = objsArray[i];

Compare results of other browsers


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:


Comment form temporarily disabled.

Add a comment