String concatenation perf

JavaScript performance comparison

Test case created

Info

See http://stackoverflow.com/questions/9538678/fastest-way-to-build-this-string

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var items = [{
        html: '<div>test</div>'
    }, {
        html: '<div>test</div>'
    }, {
        html: '<div>test</div>'
    }];
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
join
var html = [];
for (var i = 0, itemLen = items.length; i < itemLen; i++) {
    html.push(items[i].html)
}
var str = html.join('');
pending…
concatenation
var html = '';
for (var i = 0, itemLen = items.length; i < itemLen; i++) {
    html += items[i].html;
}
pending…
optimized array
var html = new Array(items.length)
for (var i = 0, itemLen = items.length; i < itemLen; i++) {
    html[i] = items[i].html;
}
var str = html.join('');
pending…
map join
var html = items.map(function(x) { return x.html; }).join('');
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