String concat + single append vs multiple append

JavaScript performance comparison

Revision 13 of this test case created by Robin Cloutman

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<style type="text/css">
  .container { height: 50px; width:100%; overflow:hidden}
</style>
<div class="container">
</div>
<script>
  var cnt = $('.container');
</script>
      
<script>
Benchmark.prototype.setup = function() {
  cnt.empty();

};
</script>

Preparation code output

<style type="text/css"> .container { height: 50px; width:100%; overflow:hidden} </style> <div class="container"> </div> <script> var cnt = $('.container'); </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
String concat + single append
var str = '<div class="sample">test</div>';
str += '<div class="sample2">test</div>';
str += '<div class="sample3">test</div>';
str += '<div class="sample4">test</div>';
str += '<div class="sample5">test</div>';

$('.container').append(str);
pending…
Multiple append
$('.container')
  .append('<div class="sample">test</div>');
$('.container')
  .append('<div class="sample1">test</div>');
$('.container')
  .append('<div class="sample2">test</div>');
$('.container')
  .append('<div class="sample3">test</div>');
$('.container')
  .append('<div class="sample4">test</div>');
pending…
append fragments array
var str = [];
str.push('<div class="sample">test</div>');
str.push( '<div class="sample2">test</div>');
str.push( '<div class="sample3">test</div>');
str.push( '<div class="sample4">test</div>');
str.push( '<div class="sample5">test</div>');

$('.container').append(str.join(''));
pending…
append fragments array apply
var str = [];
str.push('<div class="sample">test</div>');
str.push( '<div class="sample2">test</div>');
str.push( '<div class="sample3">test</div>');
str.push( '<div class="sample4">test</div>');
str.push( '<div class="sample5">test</div>');

$.fn.append.apply($('.container'), str);
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