From 434e23c2096fc723364705d130dd19c32431e6e5 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Mon, 2 Jul 2012 02:59:33 -0400 Subject: [PATCH] Add `_.template` benchmarks. Former-commit-id: 97c5a90825d6a9a0b876d8c9f90e621f00389766 --- perf/index.html | 4 +- perf/perf.js | 150 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+), 2 deletions(-) diff --git a/perf/index.html b/perf/index.html index ef3833cc6..a51c34823 100644 --- a/perf/index.html +++ b/perf/index.html @@ -16,7 +16,7 @@ - + @@ -45,4 +45,4 @@ }; - \ No newline at end of file + diff --git a/perf/perf.js b/perf/perf.js index 9e80361e9..e18034f60 100644 --- a/perf/perf.js +++ b/perf/perf.js @@ -90,6 +90,94 @@ _boundCtor = _.bind(ctor, { 'name': 'moe' }), _boundPartial = _.bind(func, { 'name': 'moe' }, 'hi'); + var tplData = { + 'header1': 'Header1', + 'header2': 'Header2', + 'header3': 'Header3', + 'header4': 'Header4', + 'header5': 'Header5', + 'header6': 'Header6', + 'list': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'] + }; + + var tplBase = + '
' + + "

<%= header1 %>

" + + "

<%= header2 %>

" + + "

<%= header3 %>

" + + "

<%= header4 %>

" + + "
<%= header5 %>
" + + "
<%= header6 %>
"; + + var tpl = + tplBase + + "' + + '
'; + + var tplWithEvaluate = + tplBase + + "' + + ''; + + var tplBaseVerbose = + '
' + + "

<%= data.header1 %>

" + + "

<%= data.header2 %>

" + + "

<%= data.header3 %>

" + + "

<%= data.header4 %>

" + + "
<%= data.header5 %>
" + + "
<%= data.header6 %>
"; + + var tplVerbose = + tplBaseVerbose + + "' + + '
'; + + var tplVerboseWithEvaluate = + tplBaseVerbose + + "' + + ''; + + var lodashTpl = lodash.template(tpl), + lodashTplWithEvaluate = lodash.template(tplWithEvaluate), + lodashTplVerbose = lodash.template(tplVerbose, null, { 'variable': 'data' }), + lodashTplVerboseWithEvaluate = lodash.template(tplVerboseWithEvaluate, null, { 'variable': 'data' }); + + var _tpl = _.template(tpl), + _tplWithEvaluate = _.template(tplWithEvaluate), + _tplVerbose = _.template(tplVerbose, null, { 'variable': 'data' }), + _tplVerboseWithEvaluate = _.template(tplVerboseWithEvaluate, null, { 'variable': 'data' }); + var wordToNumber = { 'one': 1, 'two': 2, @@ -594,6 +682,68 @@ /*--------------------------------------------------------------------------*/ + suites.push( + Benchmark.Suite('`_.template` without "evaluate" delimiters') + .add('Lo-Dash', function() { + lodash.template(tpl, tplData); + }) + .add('Underscore', function() { + _.template(tpl, tplData); + }) + ); + + suites.push( + Benchmark.Suite('`_.template` with "evaluate" delimiters') + .add('Lo-Dash', function() { + lodash.template(tplWithEvaluate, tplData); + }) + .add('Underscore', function() { + _.template(tplWithEvaluate, tplData); + }) + ); + + suites.push( + Benchmark.Suite('compiled template without "evaluate" delimiters') + .add('Lo-Dash', function() { + lodashTpl(tplData); + }) + .add('Underscore', function() { + _tpl(tplData); + }) + ); + + suites.push( + Benchmark.Suite('compiled template with "evaluate" delimiters') + .add('Lo-Dash', function() { + lodashTplWithEvaluate(tplData); + }) + .add('Underscore', function() { + _tplWithEvaluate(tplData); + }) + ); + + suites.push( + Benchmark.Suite('compiled template without a with-statement or "evaluate" delimiters') + .add('Lo-Dash', function() { + lodashTplVerbose(tplData); + }) + .add('Underscore', function() { + _tplVerbose(tplData); + }) + ); + + suites.push( + Benchmark.Suite('compiled template without a with-statement using "evaluate" delimiters') + .add('Lo-Dash', function() { + lodashTplVerboseWithEvaluate(tplData); + }) + .add('Underscore', function() { + _tplVerboseWithEvaluate(tplData); + }) + ); + + /*--------------------------------------------------------------------------*/ + suites.push( Benchmark.Suite('`_.times`') .add('Lo-Dash', function() {