Combine DOM nodes

JavaScript performance comparison

Test case created by Bob Myers

Info

Compare performance of jQuery vs. native DOM APIs in moving the contents of one element under another.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    $("body").append('<span id="test">Lorem ipsum <span id="selected-1">dolor sit</span><span id="selected-2" title="important info here"> amet, consectetur</span> adipiscing elit.</span>');
};

Benchmark.prototype.teardown = function() {
    $("#test").remove();
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
DOM
var span1 = document.getElementById("selected-1"),
    span2 = document.getElementById("selected-2"),
    insert = span2.firstChild,
    elt;

while (elt = span1.lastChild) {
  insert = span2.insertBefore(elt, insert); // removes the node from span1
}

// This line causes jsperf to fail--why?
// span1.parentNode.removeChild(span1);
pending…
jQuery
$('#selected-2').prepend($('#selected-1').html()).prev().remove();
pending…

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

Compare results of other browsers

0 comments

Add a comment