jQuery append vs string-concat

JavaScript performance comparison

Test case created by markus

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

<div id="main">
</div>
      
<script>
Benchmark.prototype.setup = function() {
  var root = null;

};

Benchmark.prototype.teardown = function() {
  if (typeof root == 'string' || root instanceof String)
  {
  $('#main').html(root);
  }else {
  $('#main').append(root);
  }

};
</script>

Preparation code output

<div id="main"> </div>

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
append
var root = $(document.createElement('div')).addClass("myRootNode");

// Testcase 1
for (var i = 0, j = 100; i < j; i += 1) {
  var line = $(document.createElement('div')).addClass('newDiv');

  for (var k = 0, l = 100; k < l; k += 1) {
    var wrapper = $(document.createElement('div')).addClass('wrapper');
    var content = $(document.createElement('span')).addClass('content').text("Here is a lot of stuff going on");

    wrapper.append(content);
    line.append(wrapper);
  }

  root.append(line);
}
pending…
string
var root = "<div class='myRootNode'>";
// Testcase 2
for (var i = 0, j = 100; i < j; i += 1) {
  var line = "<div class='newDiv'>";

  for (var k = 0, l = 100; k < l; k += 1) {
    var content = "<div class='wrapper'><span class='content'>Here is a lot of stuff going on</span></div>";

    line += content + "</div>";
  }

  root += line;
}
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