diff --git a/benchmark/index.html b/benchmark/index.html index 529cd90..ce8191f 100644 --- a/benchmark/index.html +++ b/benchmark/index.html @@ -69,16 +69,18 @@

shootout

-

Targets: Dust, Handlebars, Mustache, jquery-tmpl. Each benchmark runs once using an adaptive test cycles algorithm similar to the one found in jslitmus.

+

Targets: Dust, Handlebars, Mustache, jquery-tmpl, Underscore. Each benchmark runs once using an adaptive test cycles algorithm similar to the one found in jslitmus.

- + + + @@ -135,6 +137,7 @@

shootout

handlebarsBench(suite, type, "handlebars"); mustacheBench(suite, type, "mustache"); jqueryBench(suite, type, "jquery"); + underscoreBench(suite, type, "underscore"); suite.run(); } diff --git a/benchmark/suites/handlebars_suite.js b/benchmark/suites/handlebars_suite.js index ae0c88d..fcd636d 100644 --- a/benchmark/suites/handlebars_suite.js +++ b/benchmark/suites/handlebars_suite.js @@ -76,7 +76,7 @@ var benches = { } } -} +}; exports.handlebarsBench = function(suite, name, id) { var bench = benches[name], @@ -94,7 +94,7 @@ exports.handlebarsBench = function(suite, name, id) { fn(ctx, {partials: partials}); next(); }); -} +}; exports.handlebarsBench.benches = benches; diff --git a/benchmark/suites/underscore_suite.js b/benchmark/suites/underscore_suite.js new file mode 100644 index 0000000..165c7a3 --- /dev/null +++ b/benchmark/suites/underscore_suite.js @@ -0,0 +1,111 @@ +(function(exports){ + +var benches = { + + string: { + source: "Hello World!", + context: {} + }, + + replace: { + source: "Hello <%= name %>! You have <%= count %> new messages.", + context: { name: "Mick", count: 30 } + }, + + array: { + source: "<% _.each(names, function(item) { %> <%= item.name %> <% }); %>", + context: { names: [{name: "Moe"}, {name: "Larry"}, {name: "Curly"}, {name: "Shemp"}] } + }, + + object: { + source: "<%= person.name %> <%= person.age %>", + context: { person: { name: "Larry", age: 45 } } + }, + + partial: { + source: "<% _.each(peeps, function(peep) { %> <%= partials.myPartialTemplate(peep) %> <% }); %>", + context: { peeps: [{name: "Moe", count: 15}, {name: "Larry", count: 5}, {name: "Curly", count: 1}] }, + partials: { myPartialTemplate: "Hello <%= name %>! You have <%= count %> new messages." } + }, + + recursion: { + source: "<%= name %> <% _.each(kids, function(kid) { %> <%= partials.recursion({data:kid, partials:partials}) %> <% }); %>", + context: { + name: '1', + kids: [ + { + name: '1.1', + kids: [ + {name: '1.1.1'} + ] + } + ] + }, + partials: { recursion: "<%= data.name %> <% _.each(data.kids, function(kid) { %> <%= partials.recursion({data:kid, partials:partials}) %> <% }); %>" } + }, + + filter: { + source: "foo <%= filter(bar) %>", + context: { + filter: function(str) { + return str.toUpperCase(); + }, + bar: "bar" + } + }, + + complex: { + source: "

<%= header() %>

\n" + + "<% if(hasItems) { %>" + + " <% _.each(items, function(item) { %>\n" + + " \n" + + " <% }); %>" + + "<% } else { %>\n" + + "

The list is empty.

\n" + + "<% } %>", + context: { + header: function() { + return "Colors"; + }, + items: [ + {name: "red", current: true, url: "#Red"}, + {name: "green", current: false, url: "#Green"}, + {name: "blue", current: false, url: "#Blue"} + ], + hasItems: function(ctx) { + return ctx.items.length ? true : false; + } + } + } + +}; + +exports.underscoreBench = function(suite, name, id) { + var bench = benches[name], + fn = _.template(bench.source), + ctx = bench.context, + partials = {}; + + if (bench.partials) { + for (var key in bench.partials) { + partials[key] = _.template(bench.partials[key]); + } + } + + ctx.partials = partials; + + suite.bench(id || name, function(next) { + fn(ctx); + next(); + }); +}; + +exports.underscoreBench.benches = benches; + +})(typeof exports !== "undefined" ? exports : window);