Substituting one tag for another with jQuery

JavaScript performance comparison

Revision 4 of this test case created

Info

From http://stackoverflow.com/questions/7093417/using-jquery-to-replace-one-tag-with-another/7093537

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="test">
<code>A</code><code>B</code><code>C</code><code>D</code><code>E</code>
<code>F</code><code>G</code><code>H</code><code>I</code><code>J</code>
<code>A</code><code>B</code><code>C</code><code>D</code><code>E</code>
<code>F</code><code>G</code><code>H</code><code>I</code><code>J</code>
<code>A</code><code>B</code><code>C</code><code>D</code><code>E</code>
<code>F</code><code>G</code><code>H</code><code>I</code><code>J</code>
<code>A</code><code>B</code><code>C</code><code>D</code><code>E</code>
<code>F</code><code>G</code><code>H</code><code>I</code><code>J</code>
<code>A</code><code>B</code><code>C</code><code>D</code><code>E</code>
<code>F</code><code>G</code><code>H</code><code>I</code><code>J</code>

<code>
<span>A</span><span>B</span><span>C</span><span>D</span><span>E</span>
<span>F</span><span>G</span><span>H</span><span>I</span><span>J</span>
</code>
<code>
<span>A</span><span>B</span><span>C</span><span>D</span><span>E</span>
<span>F</span><span>G</span><span>H</span><span>I</span><span>J</span>
</code>
<code>
<span>A</span><span>B</span><span>C</span><span>D</span><span>E</span>
<span>F</span><span>G</span><span>H</span><span>I</span><span>J</span>
</code>
<code>
<span>A</span><span>B</span><span>C</span><span>D</span><span>E</span>
<span>F</span><span>G</span><span>H</span><span>I</span><span>J</span>
</code>
<code>
<span>A</span><span>B</span><span>C</span><span>D</span><span>E</span>
<span>F</span><span>G</span><span>H</span><span>I</span><span>J</span>
</code>
</div>
<script>
  var $dom = $("#test").detach().addClass('test-div');
  var $content = $('.user-output');
  var $test = $();
</script>
<script>
Benchmark.prototype.setup = function() {
    $test.remove();
    $test = $dom.clone();
   
    $content.append($test);
};
</script>

Preparation code output

ABCDE FGHIJ ABCDE FGHIJ ABCDE FGHIJ ABCDE FGHIJ ABCDE FGHIJ ABCDE FGHIJ ABCDE FGHIJ ABCDE FGHIJ ABCDE FGHIJ ABCDE FGHIJ

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
wrap-unwrap
$('#test code').contents().unwrap().wrap('<div/>');
pending…
replaceWith string
$('#test code').each(function() {
  $(this).replaceWith( "<div>" + $(this).html() + "</div>" );
});
pending…
replaceWith function
$('#test code').replaceWith(function(){
  return $("<div />", {html: $(this).html()});
});
pending…
replaceWith function + DOM manipulation
$('#test code').replaceWith(function(){
  return $("<div />").append($(this).contents());
});
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