underscore.js template vs. string concatenation

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

<script src="http://documentcloud.github.com/underscore/underscore-min.js">
</script>
<script>
Benchmark.prototype.setup = function() {
    var template = _.template("body.search #main, body.project #main, #sidebar { height: {{ height }}px }");
    var height = 320;
    var sprintf = function(str) {
        var i, substitutions;
        i = 0;
        substitutions = Array.prototype.slice.call(arguments).slice(1);
        return str.replace(/%s/g, function() {
            return substitutions[i++] || '';
        });
    }
    var tidyjoin = function() {
       return Array.prototype.slice.call(arguments).join('');
    }
};
</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
string concatenation
"body.search #main, body.project #main, #sidebar { height: " + height + "px }";
pending…
underscore.js template
template({ height: height })
pending…
sprintf
sprintf("body.search #main, body.project #main, #sidebar { height: %spx }", height);
pending…
tidyjoin
tidyjoin("body.search #main, body.project #main, #sidebar { height: ", height, "px }");
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