diff --git a/TODO.markdown b/TODO.markdown deleted file mode 100644 index 1e83a163..00000000 --- a/TODO.markdown +++ /dev/null @@ -1,8 +0,0 @@ -* README -* Rationale (how it's different from mustache, and why) - -* Refactor blocks/inverted sections so shared code isn't copied and pasted - -* Add support for {{^}} when helperMissing is in play. -* Figure out how to allow detection of first/last/iteration when enumerating over arrays. -* Easier way to push context within a block helper, so that it can pass a built context to its function. diff --git a/lib/handlebars.js b/lib/handlebars.js index 7df35481..26dd7b79 100644 --- a/lib/handlebars.js +++ b/lib/handlebars.js @@ -1,20 +1,27 @@ var Handlebars = { + compilerCache: {}, + compile: function(string) { - var fnBody = Handlebars.compileFunctionBody(string); - var fn = new Function("context", "fallback", "Handlebars", fnBody); - return function(context, fallback) { return fn(context, fallback, Handlebars); }; + if (Handlebars.compilerCache[string] == null) { + var fnBody = Handlebars.compileFunctionBody(string); + var fn = new Function("context", "fallback", "Handlebars", fnBody); + Handlebars.compilerCache[string] = + function(context, fallback) { return fn(context, fallback, Handlebars); }; + } + + return Handlebars.compilerCache[string]; }, compileToString: function(string) { var fnBody = Handlebars.compileFunctionBody(string); return "function(context, fallback) { " + fnBody + "}"; }, - + compileFunctionBody: function(string) { - var compiler = new Handlebars.Compiler(string); - compiler.compile(); + var compiler = new Handlebars.Compiler(string); + compiler.compile(); - return "fallback = fallback || {}; var stack = [];" + compiler.fn; + return "fallback = fallback || {}; var stack = [];" + compiler.fn; }, isFunction: function(fn) { @@ -62,17 +69,11 @@ var Handlebars = { }); }, - compiledPartials: {}, compilePartial: function(partial) { - var compiled = Handlebars.compiledPartials[partial]; - - if (compiled == null) { - if (Handlebars.isFunction(partial)) { - compiled = partial; - } else { - compiled = Handlebars.compile(partial); - } - Handlebars.compiledPartials[partial] = compiled; + if (Handlebars.isFunction(partial)) { + compiled = partial; + } else { + compiled = Handlebars.compile(partial); } return compiled; @@ -191,9 +192,7 @@ var Handlebars = { out = out + Handlebars.helperMissing.not.call(arg, lookup, notFn); } } - - return out; }, diff --git a/test/perf.js b/test/perf.js index 052c9431..a59d59c4 100644 --- a/test/perf.js +++ b/test/perf.js @@ -79,9 +79,8 @@ var data = { ] } -var fn = Handlebars.compile(tmpl); var test1 = function() { - fn(data, {partials: partials}); + Handlebars.compile(tmpl)(data, {partials: partials}); } var test2 = function() { Mustache.to_html(tmpl, data, partials);