jQuery replaceWith vs after plus remove

Benchmark created by Stephen on


Description

This tests if it is faster to use jQuery replaceWith or a combination of after() and then remove() to replace a DOM node with a different node.

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<span class="image">&nbsp;</span>
<span class="image">&nbsp;</span>

Setup

var node = '<span class="image">&nbsp;</span>';

Test runner

Ready to run.

Testing in
TestOps/sec
replaceWith
$('.image').each(function () {
    $(this).replaceWith(node);
});
ready
after then remove
$('.image').each(function () {
    $(this).after(node).remove();
});
ready

Revisions

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

  • Revision 1: published by Stephen on