From Nodelist to Array

JavaScript performance comparison

Revision 6 of this test case created

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'), slice = Array.prototype.slice;
};
</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
slice, instance method
[].slice.call(nodes);
pending…
slice, prototype call
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. Here’s a list of current revisions for this page:

0 comments

Add a comment