From 0ea89d2c2c284d3efa93347ac06c0933fb3aca5b Mon Sep 17 00:00:00 2001 From: wycats Date: Sun, 30 Jan 2011 12:21:18 -0800 Subject: [PATCH 1/4] Get specs up to snuff --- Rakefile | 16 ++++++++++++- bench/benchwarmer.js | 15 ++++++------ bench/handlebars.js | 56 ++++++++++++++++++++++++++++++++++---------- 3 files changed, 66 insertions(+), 21 deletions(-) diff --git a/Rakefile b/Rakefile index 822af224..d78f1acb 100644 --- a/Rakefile +++ b/Rakefile @@ -73,8 +73,10 @@ task :debug => [:compile, "dist/handlebars.debug.js"] desc "build the build, debug and base versions of handlebars" task :release => [:build, :debug, :base] +directory "vendor" + desc "benchmark against dust.js and mustache.js" -task :bench do +task :bench => "vendor" do require "open-uri" File.open("vendor/mustache.js", "w") do |file| file.puts open("https://github.com/janl/mustache.js/raw/master/mustache.js").read @@ -91,5 +93,17 @@ task :bench do system "git clone git://github.com/akdubya/dustjs.git vendor/dustjs" end + if File.directory?("vendor/coffee") + system "cd vendor/coffee && git pull" + else + system "git clone git://github.com/jashkenas/coffee-script.git vendor/coffee" + end + + if File.directory?("vendor/eco") + system "cd vendor/eco && git pull" + else + system "git clone git://github.com/sstephenson/eco.git vendor/eco" + end + system "node bench/handlebars.js" end diff --git a/bench/benchwarmer.js b/bench/benchwarmer.js index c90fe01d..3ea463d5 100644 --- a/bench/benchwarmer.js +++ b/bench/benchwarmer.js @@ -1,4 +1,4 @@ -var print = require("sys").print; + var Benchmark = require("benchmark"); var BenchWarmer = function(names) { @@ -8,6 +8,8 @@ var BenchWarmer = function(names) { this.errors = {}; }; +var print = require("sys").print; + BenchWarmer.prototype = { winners: function(benches) { var result = Benchmark.filter(benches, function(bench) { return bench.cycles; }); @@ -56,8 +58,8 @@ BenchWarmer.prototype = { if(first) { self.startLine(suiteName); } self.writeBench(bench); self.currentBenches.push(bench); - }, onError: function(bench) { - self.errors[bench.name] = bench; + }, onError: function() { + self.errors[this.name] = this; } }); @@ -89,7 +91,7 @@ BenchWarmer.prototype = { print("\n" + new Array(horSize + 1).join("-")); Benchmark.invoke(this.benchmarks, { - methodName: "run", + name: "run", onComplete: function() { var errors = false, prop, bench; for(prop in self.errors) { if(self.errors.hasOwnProperty(prop)) { errors = true; break; } } @@ -129,10 +131,9 @@ BenchWarmer.prototype = { if(!bench.error) { var count = bench.hz, - min = count - bench.MoE, - max = count + bench.MoE; + moe = count * bench.stats.RME / 100; - out = Math.round(count / 1000) + " ±" + Math.round(bench.MoE / 1000) + " (" + bench.cycles + ")"; + out = Math.round(count / 1000) + " ±" + Math.round(moe / 1000) + " (" + bench.cycles + ")"; } else { out = "E"; } diff --git a/bench/handlebars.js b/bench/handlebars.js index 3eebdc98..1d51937b 100644 --- a/bench/handlebars.js +++ b/bench/handlebars.js @@ -1,25 +1,38 @@ require.paths.push("lib"); require.paths.push("vendor"); require.paths.push("vendor/dustjs/lib"); +require.paths.push("vendor/coffee/lib"); +require.paths.push("vendor/eco/lib"); + var BenchWarmer = require("./benchwarmer"); -Handlebars = require("handlebars").Handlebars; +Handlebars = require("handlebars"); var dust = require("dust"); var Mustache = require("mustache"); +var ecoExports = require("eco"); + +eco = function(str) { + var module = {}; + var template = new Function("module", ecoExports.compile(str)); + template(module); + return module.exports; +} var benchDetails = { string: { context: {}, handlebars: "Hello world", dust: "Hello world", - mustache: "Hello world" + mustache: "Hello world", + eco: "Hello world" }, variables: { context: {name: "Mick", count: 30}, handlebars: "Hello {{name}}! You have {{count}} new messages.", dust: "Hello {name}! You have {count} new messages.", - mustache: "Hello {{name}}! You have {{count}} new messages." + mustache: "Hello {{name}}! You have {{count}} new messages.", + eco: "Hello <%= @name %>! You have <%= @count %> new messages." }, object: { context: { person: { name: "Larry", age: 45 } }, @@ -31,7 +44,8 @@ var benchDetails = { context: { names: [{name: "Moe"}, {name: "Larry"}, {name: "Curly"}, {name: "Shemp"}] }, handlebars: "{{#each names}}{{name}}{{/each}}", dust: "{#names}{name}{/names}", - mustache: "{{#names}}{{name}}{{/names}}" + mustache: "{{#names}}{{name}}{{/names}}", + eco: "<% for item in @names: %><%= item.name %><% end %>" }, partial: { context: { peeps: [{name: "Moe", count: 15}, {name: "Larry", count: 5}, {name: "Curly", count: 1}] }, @@ -88,6 +102,7 @@ var benchDetails = { }; handlebarsTemplates = {}; +ecoTemplates = {}; var warmer = new BenchWarmer(); @@ -99,26 +114,41 @@ var makeSuite = function(name) { var mustacheSource = details.mustache; var context = details.context; - bench("dust", function() { - dust.render(templateName, context, function(err, out) { }); - }); + var error = function() { throw new Error("EWOT"); }; + + + //bench("dust", function() { + //dust.render(templateName, context, function(err, out) { }); + //}); bench("handlebars", function() { handlebarsTemplates[templateName](context); }); - if(mustacheSource) { - bench("mustache", function() { - Mustache.to_html(mustacheSource, context, mustachePartials); - }); - } + //if(ecoTemplates[templateName]) { + //bench("eco", function() { + //ecoTemplates[templateName](context); + //}); + //} else { + //bench("eco", error); + //} + + //if(mustacheSource) { + //bench("mustache", function() { + //Mustache.to_html(mustacheSource, context, mustachePartials); + //}); + //} else { + //bench("mustache", error); + //} }); } for(var name in benchDetails) { if(benchDetails.hasOwnProperty(name)) { dust.loadSource(dust.compile(benchDetails[name].dust, name)); - handlebarsTemplates[name] = Handlebars.VM.compile(benchDetails[name].handlebars); + handlebarsTemplates[name] = Handlebars.compile(benchDetails[name].handlebars); + + if(benchDetails[name].eco) { ecoTemplates[name] = eco(benchDetails[name].eco); } var partials = benchDetails[name].partials; if(partials) { From 4d501b661320fda86802f3ac4d8ab90322b1d09f Mon Sep 17 00:00:00 2001 From: wycats Date: Mon, 7 Feb 2011 19:41:35 -0800 Subject: [PATCH 2/4] Need to require timeout --- spec/tokenizer_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/tokenizer_spec.rb b/spec/tokenizer_spec.rb index fba7c318..8f39707a 100644 --- a/spec/tokenizer_spec.rb +++ b/spec/tokenizer_spec.rb @@ -1,4 +1,5 @@ require "spec_helper" +require "timeout" describe "Tokenizer" do let(:parser) { @context["handlebars"] } From aaaafb4400ed88e955d97f73372309748c606784 Mon Sep 17 00:00:00 2001 From: wycats Date: Tue, 8 Feb 2011 14:31:09 -0800 Subject: [PATCH 3/4] Makes Handlebars work in contexts without a global object (node) --- lib/handlebars/vm.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/handlebars/vm.js b/lib/handlebars/vm.js index b8a33b81..5013b7ae 100644 --- a/lib/handlebars/vm.js +++ b/lib/handlebars/vm.js @@ -345,7 +345,7 @@ Handlebars.JavaScriptCompiler = function() {}; this.source.push("return buffer;"); - var params = ["context", "helpers", "partials"]; + var params = ["Handlebars", "context", "helpers", "partials"]; for(var i=0, l=this.environment.depths.list.length; i Date: Tue, 8 Feb 2011 15:01:28 -0800 Subject: [PATCH 4/4] Fix broken README information --- README.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index 02f244a4..9501b243 100644 --- a/README.markdown +++ b/README.markdown @@ -5,7 +5,7 @@ Handlebars.js is an extension to the [Mustache templating language](http://musta Installing ---------- -Installing Handlebars is easy. Simply [download the package from GitHub](https://github.com/downloads/wycats/handlebars.js/handlebars.js) and add it to your web pages. You can also get a [debug version](https://github.com/downloads/wycats/handlebars.js/handlebars.debug.js) which helps newer browsers print out useful stack traces. +Installing Handlebars is easy. Simply [download the package from GitHub](https://github.com/wycats/handlebars.js/archives/master) and add it to your web pages (you should usually use the most recent version). Usage -----