Underscore templates - classic vs. precompiled

JavaScript performance comparison

Revision 19 of this test case created

Info

Benchmarking difference in performance of rendering simple Underscore template - classic vs. precompiled

Preparation code

<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/1.0.0-rc.2/lodash.min.js"></script>

<script>
  var lodash = _.noConflict();
</script>
 
<script>
Benchmark.prototype.setup = function() {
    var _ = window.lodash;
   
    var template = '<div class="item <%= id %>">' +
      '  <h1><%= title %></h1>' +
      '  <p><%= desc %></p>' +
      '  <ul class="attributes">' +
      '    <li><%= attrs.dimensions %></li>' +
      '    <li><%= attrs.weight %></li>' +
      '    <li><%= attrs.price %></li>' +
      '  </ul>' +
      '</div>'
    '<div class="item <%= id1 %>">' +
      '  <h1><%= title1 %></h1>' +
      '  <p><%= desc1 %></p>' +
      '  <ul class="attributes">' +
      '    <li><%= attrs.dimensions %></li>' +
      '    <li><%= attrs.weight %></li>' +
      '    <li><%= attrs.price %></li>' +
      '  </ul>' +
      '</div>';
    var templateNoWith = '<div class="item <%= data.id %>">' +
      '  <h1><%= data.title %></h1>' +
      '  <p><%= data.desc %></p>' +
      '  <ul class="attributes">' +
      '    <li><%= data.attrs.dimensions %></li>' +
      '    <li><%= data.attrs.weight %></li>' +
      '    <li><%= data.attrs.price %></li>' +
      '  </ul>' +
      '</div>'
    '<div class="item <%= data.id1 %>">' +
      '  <h1><%= data.title1 %></h1>' +
      '  <p><%= data.desc1 %></p>' +
      '  <ul class="attributes1">' +
      '    <li><%= data.attrs.dimensions %></li>' +
      '    <li><%= data.attrs.weight %></li>' +
      '    <li><%= data.attrs.price %></li>' +
      '  </ul>' +
      '</div>';
   
    var templateFuncPrecompiled = eval("(" + _.template(template).source + ")");
    var templateFuncPrecompiledNoWith = eval("(" + _.template(templateNoWith, null, {
      'variable': 'data'
    }).source + ")");
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
classic
var output = _.template(template, { // render template
  id: 5,
  title: 'Hello World!',
  desc: 'Ordianry item',
  id1: 5,
  title1: 'Hello World!',
  desc1: 'Ordianry item',
  attrs: {
    dimensions: '100cm x 100cm x 20cm',
    weight: '15kg',
    price: '10EUR',
    dimensions1: '100cm x 100cm x 20cm',
    weight2: '15kg',
    price3: '10EUR'


  },
  attrs1: {
    dimensions: '100cm x 100cm x 20cm',
    weight: '15kg',
    price: '10EUR',
    dimensions1: '100cm x 100cm x 20cm',
    weight2: '15kg',
    price3: '10EUR'


  },
  "configGlossary:installationAt": "Philadelphia, PA",
  "configGlossary:adminEmail": "ksm@pobox.com",
  "configGlossary:poweredBy": "Cofax",
  "configGlossary:poweredByIcon": "/images/cofax.gif",
  "configGlossary:staticPath": "/content/static",
  "templateProcessorClass": "org.cofax.WysiwygTemplate",
  "templateLoaderClass": "org.cofax.FilesTemplateLoader",
  "templatePath": "templates",
  "templateOverridePath": "",
  "defaultListTemplate": "listTemplate.htm",
  "defaultFileTemplate": "articleTemplate.htm",
  "useJSP": false,
  "jspListTemplate": "listTemplate.jsp",
  "jspFileTemplate": "articleTemplate.jsp",
  "cachePackageTagsTrack": 200,
  "cachePackageTagsStore": 200,
  "cachePackageTagsRefresh": 60,
  "cacheTemplatesTrack": 100,
  "cacheTemplatesStore": 50,
  "cacheTemplatesRefresh": 15,
  "cachePagesTrack": 200,
  "cachePagesStore": 100,
  "cachePagesRefresh": 10,
  "cachePagesDirtyRead": 10,
  "searchEngineListTemplate": "forSearchEnginesList.htm",
  "searchEngineFileTemplate": "forSearchEngines.htm",
  "searchEngineRobotsDb": "WEB-INF/robots.db",
  "useDataStore": true,
  "dataStoreClass": "org.cofax.SqlDataStore",
  "redirectionClass": "org.cofax.SqlRedirection",
  "dataStoreName": "cofax",
  "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
  "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
  "dataStoreUser": "sa",
  "dataStorePassword": "dataStoreTestQuery",
  "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
  "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
  "dataStoreInitConns": 10,
  "dataStoreMaxConns": 100,
  "dataStoreConnUsageLimit": 100,
  "dataStoreLogLevel": "debug",
  "maxUrlLength": 500

});
pending…
precompiled
var output = templateFuncPrecompiled({ // render template
  id: 5,
  title: 'Hello World!',
  desc: 'Ordianry item',
  id1: 5,
  title1: 'Hello World!',
  desc1: 'Ordianry item',
  attrs: {
    dimensions: '100cm x 100cm x 20cm',
    weight: '15kg',
    price: '10EUR',
    dimensions1: '100cm x 100cm x 20cm',
    weight2: '15kg',
    price3: '10EUR'


  },
  attrs1: {
    dimensions: '100cm x 100cm x 20cm',
    weight: '15kg',
    price: '10EUR',
    dimensions1: '100cm x 100cm x 20cm',
    weight2: '15kg',
    price3: '10EUR'


  },
  "configGlossary:installationAt": "Philadelphia, PA",
  "configGlossary:adminEmail": "ksm@pobox.com",
  "configGlossary:poweredBy": "Cofax",
  "configGlossary:poweredByIcon": "/images/cofax.gif",
  "configGlossary:staticPath": "/content/static",
  "templateProcessorClass": "org.cofax.WysiwygTemplate",
  "templateLoaderClass": "org.cofax.FilesTemplateLoader",
  "templatePath": "templates",
  "templateOverridePath": "",
  "defaultListTemplate": "listTemplate.htm",
  "defaultFileTemplate": "articleTemplate.htm",
  "useJSP": false,
  "jspListTemplate": "listTemplate.jsp",
  "jspFileTemplate": "articleTemplate.jsp",
  "cachePackageTagsTrack": 200,
  "cachePackageTagsStore": 200,
  "cachePackageTagsRefresh": 60,
  "cacheTemplatesTrack": 100,
  "cacheTemplatesStore": 50,
  "cacheTemplatesRefresh": 15,
  "cachePagesTrack": 200,
  "cachePagesStore": 100,
  "cachePagesRefresh": 10,
  "cachePagesDirtyRead": 10,
  "searchEngineListTemplate": "forSearchEnginesList.htm",
  "searchEngineFileTemplate": "forSearchEngines.htm",
  "searchEngineRobotsDb": "WEB-INF/robots.db",
  "useDataStore": true,
  "dataStoreClass": "org.cofax.SqlDataStore",
  "redirectionClass": "org.cofax.SqlRedirection",
  "dataStoreName": "cofax",
  "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
  "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
  "dataStoreUser": "sa",
  "dataStorePassword": "dataStoreTestQuery",
  "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
  "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
  "dataStoreInitConns": 10,
  "dataStoreMaxConns": 100,
  "dataStoreConnUsageLimit": 100,
  "dataStoreLogLevel": "debug",
  "maxUrlLength": 500

});
pending…
NoWith
var output = templateFuncPrecompiledNoWith({ // render template
  id: 5,
  title: 'Hello World!',
  desc: 'Ordianry item',
  id1: 5,
  title1: 'Hello World!',
  desc1: 'Ordianry item',
  attrs: {
    dimensions: '100cm x 100cm x 20cm',
    weight: '15kg',
    price: '10EUR',
    dimensions1: '100cm x 100cm x 20cm',
    weight2: '15kg',
    price3: '10EUR'


  },
  attrs1: {
    dimensions: '100cm x 100cm x 20cm',
    weight: '15kg',
    price: '10EUR',
    dimensions1: '100cm x 100cm x 20cm',
    weight2: '15kg',
    price3: '10EUR'


  },
  "configGlossary:installationAt": "Philadelphia, PA",
  "configGlossary:adminEmail": "ksm@pobox.com",
  "configGlossary:poweredBy": "Cofax",
  "configGlossary:poweredByIcon": "/images/cofax.gif",
  "configGlossary:staticPath": "/content/static",
  "templateProcessorClass": "org.cofax.WysiwygTemplate",
  "templateLoaderClass": "org.cofax.FilesTemplateLoader",
  "templatePath": "templates",
  "templateOverridePath": "",
  "defaultListTemplate": "listTemplate.htm",
  "defaultFileTemplate": "articleTemplate.htm",
  "useJSP": false,
  "jspListTemplate": "listTemplate.jsp",
  "jspFileTemplate": "articleTemplate.jsp",
  "cachePackageTagsTrack": 200,
  "cachePackageTagsStore": 200,
  "cachePackageTagsRefresh": 60,
  "cacheTemplatesTrack": 100,
  "cacheTemplatesStore": 50,
  "cacheTemplatesRefresh": 15,
  "cachePagesTrack": 200,
  "cachePagesStore": 100,
  "cachePagesRefresh": 10,
  "cachePagesDirtyRead": 10,
  "searchEngineListTemplate": "forSearchEnginesList.htm",
  "searchEngineFileTemplate": "forSearchEngines.htm",
  "searchEngineRobotsDb": "WEB-INF/robots.db",
  "useDataStore": true,
  "dataStoreClass": "org.cofax.SqlDataStore",
  "redirectionClass": "org.cofax.SqlRedirection",
  "dataStoreName": "cofax",
  "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
  "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
  "dataStoreUser": "sa",
  "dataStorePassword": "dataStoreTestQuery",
  "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
  "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
  "dataStoreInitConns": 10,
  "dataStoreMaxConns": 100,
  "dataStoreConnUsageLimit": 100,
  "dataStoreLogLevel": "debug",
  "maxUrlLength": 500

});
pending…
classic no with
var output = _.template(templateNoWith, { // render template
  id: 5,
  title: 'Hello World!',
  desc: 'Ordianry item',
  id1: 5,
  title1: 'Hello World!',
  desc1: 'Ordianry item',
  attrs: {
    dimensions: '100cm x 100cm x 20cm',
    weight: '15kg',
    price: '10EUR',
    dimensions1: '100cm x 100cm x 20cm',
    weight2: '15kg',
    price3: '10EUR'


  },
  attrs1: {
    dimensions: '100cm x 100cm x 20cm',
    weight: '15kg',
    price: '10EUR',
    dimensions1: '100cm x 100cm x 20cm',
    weight2: '15kg',
    price3: '10EUR'


  },
  "configGlossary:installationAt": "Philadelphia, PA",
  "configGlossary:adminEmail": "ksm@pobox.com",
  "configGlossary:poweredBy": "Cofax",
  "configGlossary:poweredByIcon": "/images/cofax.gif",
  "configGlossary:staticPath": "/content/static",
  "templateProcessorClass": "org.cofax.WysiwygTemplate",
  "templateLoaderClass": "org.cofax.FilesTemplateLoader",
  "templatePath": "templates",
  "templateOverridePath": "",
  "defaultListTemplate": "listTemplate.htm",
  "defaultFileTemplate": "articleTemplate.htm",
  "useJSP": false,
  "jspListTemplate": "listTemplate.jsp",
  "jspFileTemplate": "articleTemplate.jsp",
  "cachePackageTagsTrack": 200,
  "cachePackageTagsStore": 200,
  "cachePackageTagsRefresh": 60,
  "cacheTemplatesTrack": 100,
  "cacheTemplatesStore": 50,
  "cacheTemplatesRefresh": 15,
  "cachePagesTrack": 200,
  "cachePagesStore": 100,
  "cachePagesRefresh": 10,
  "cachePagesDirtyRead": 10,
  "searchEngineListTemplate": "forSearchEnginesList.htm",
  "searchEngineFileTemplate": "forSearchEngines.htm",
  "searchEngineRobotsDb": "WEB-INF/robots.db",
  "useDataStore": true,
  "dataStoreClass": "org.cofax.SqlDataStore",
  "redirectionClass": "org.cofax.SqlRedirection",
  "dataStoreName": "cofax",
  "dataStoreDriver": "com.microsoft.jdbc.sqlserver.SQLServerDriver",
  "dataStoreUrl": "jdbc:microsoft:sqlserver://LOCALHOST:1433;DatabaseName=goon",
  "dataStoreUser": "sa",
  "dataStorePassword": "dataStoreTestQuery",
  "dataStoreTestQuery": "SET NOCOUNT ON;select test='test';",
  "dataStoreLogFile": "/usr/local/tomcat/logs/datastore.log",
  "dataStoreInitConns": 10,
  "dataStoreMaxConns": 100,
  "dataStoreConnUsageLimit": 100,
  "dataStoreLogLevel": "debug",
  "maxUrlLength": 500

}, {
  variable: 'data'
});
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