jQuery loop remove vs batch remove (v3)

Revision 3 of this benchmark created on


Description

See whether looping through elements and removing them individually compares to putting all elements in a jQuery selection and removing all at once.

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="nodes">all the nodes</div>

Setup

var nodes = [],
        $nodes = $('#nodes');
    for (var i = 0; i < 150; i++) {
        nodes.push($('<div>hello</div>').appendTo($nodes));
    }

Test runner

Ready to run.

Testing in
TestOps/sec
loop delete
for (var i = 0, len = nodes.length; i < len; i++) {
    nodes[i].remove();
}
ready
batch delete
var $stuff = $();
for (var i = 0, len = nodes.length; i < len; i++) {
    $stuff = $stuff.add(nodes[i]);
}
$stuff.remove();
ready

Revisions

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