Substituting one tag for another with jQuery

JavaScript performance comparison

Test case created by Jens Roland

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>

<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>
 

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
$('code').contents().unwrap().wrap('<div/>');
pending…
replaceWith string
$('code').each(function() {
  $(this).replaceWith( "<div>" + $(this).html() + "</div>" );
});
pending…
replaceWith function
$('code').replaceWith(function(){
  return $("<div />", {html: $(this).html()});
});
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