node-collection-to-array

Benchmark created by Dmitry Podgorniy on


Description

Fastest way to convert array-like object to array

Preparation HTML

<!-- 20 links -->
<a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a> <a href="#">link</a>

Setup

var live_collection = document.links;
    var get_by_tag = document.getElementsByTagName('a');
    var dead_collection = document.querySelectorAll('a');

Test runner

Ready to run.

Testing in
TestOps/sec
Iterate
function to_array (obj) {
        var i, length, res;

        length = obj.length;
        res = [];
        for (i = 0; i < length; i += 1) {
                res.push(obj[i]);
        }
        return res;
}
to_array(live_collection)
ready
slice
function to_array (obj) {
        return Array.prototype.slice.call(obj);
}

to_array(live_collection);
ready
iterate (live collection)
function to_array (obj) {
        var i, length, res;

        length = obj.length;
        res = [];
        for (i = 0; i < length; i += 1) {
                res.push(obj[i]);
        }
        return res;
}
to_array(get_by_tag)
ready
iterate (dead collection)
function to_array (obj) {
        var i, length, res;

        length = obj.length;
        res = [];
        for (i = 0; i < length; i += 1) {
                res.push(obj[i]);
        }
        return res;
}
to_array(dead_collection)
ready
slice (live collection)
function to_array (obj) {
        return Array.prototype.slice.call(obj);
}

to_array(get_by_tag);
ready
slice (dead collection)
function to_array (obj) {
        return Array.prototype.slice.call(obj);
}

to_array(dead_collection);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Dmitry Podgorniy on