Substituting one tag for another with jQuery

JavaScript performance comparison

Revision 4 of this test case created by

Preparation code

<script src="https://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

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

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
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.

0 Comments

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