From Nodelist to Array

JavaScript performance comparison

Revision 5 of this test case created by

Preparation code

<div>
	<ul>
		<li id="item-2"><a href="#">item 2</a></li>
		<li id="item-1"><a href="#">item 1</a></li>
		<li id="item-3"><a href="#">item 3</a></li>
	</ul>
</div>
      
<script>
Benchmark.prototype.setup = function() {
  var nodes = document.getElementsByTagName('li');

};
</script>

Preparation code output

<div> <ul> <li id="item-2"><a href="#">item 2</a></li> <li id="item-1"><a href="#">item 1</a></li> <li id="item-3"><a href="#">item 3</a></li> </ul> </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
slice, instance method
[].slice.call(nodes);
pending…
slice, prototype call
Array.prototype.slice.call(nodes);
pending…
for loop (1)
for (var i=0, len=nodes.length, result=[]; i<len; i++) {
  result.push(nodes[i]);
}
pending…
for loop (2)
for (var i = nodes.length, result = []; i--; result.unshift(nodes[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.

0 Comments