Underscore template function with variable setting

JavaScript performance comparison

Test case created by Antti Ahti

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  var data = {
     var1: "foo",
     var2: "bar",
     list: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
    };
  
  var underscoreTemplate = _.template("<div><%= var1 %></div><div><%= var2 %></div><ul><% for (var i = 0, l = list.length; i < l; i++) { %><li><%= list[i] %></li><% } %></ul>");
    
  var underscoreTemplateVariable = _.template("<div><%= data.var1 %></div><div><%= data.var2 %></div><ul><% for (var i = 0, l = data.list.length; i < l; i++) { %><li><%= data.list[i] %></li><% } %></ul>", null, {variable: 'data'});

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
no variable
underscoreTemplate(data);
pending…
variable setting
underscoreTemplateVariable(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.

0 Comments