Template compiling

JavaScript performance comparison

Test case created by wared

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function templatize(string) {
        var me = this; // the data source
        return string.replace(/{{(.*?)}}/g, function (full, key) {
            // "this" refers to the string itself
            return me[key] || full;
        });
    }
    function compile(tpl) {
        var i = 0, tmp = [];
        tpl = tpl.split(/{{([^{}]+)}}/);
        while (i < tpl.length) {
            if (i % 2) tmp.push('this["' + tpl[i] + '"]');
            else if (tpl[i]) tmp.push('"' + tpl[i].replace(/"/g, '\\"') + '"');
            i++;
        }
        return new Function(
            'return [' + tmp.join() + '].join("");'
        );
    }
   
    var tpl = '{{a}} {{q}}';
    var compiled = compile(tpl);
};
</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
without compiling
templatize.call({ a: 'azerty', q: 'qwerty' }, tpl)
pending…
with compiling
compiled.call({ a: 'azerty', q: 'qwerty' })
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