From b60aa6f0f1a1f4ac347b47a3596fa1ba497113a9 Mon Sep 17 00:00:00 2001 From: Michael Jackson Date: Tue, 5 Nov 2013 07:04:48 -0800 Subject: [PATCH] Pre-cache view for dot references --- mustache.js | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/mustache.js b/mustache.js index a7d42e0..e27dc6a 100644 --- a/mustache.js +++ b/mustache.js @@ -123,7 +123,7 @@ function Context(view, parent) { this.view = view == null ? {} : view; this.parent = parent; - this._cache = {}; + this._cache = { '.': this.view }; } Context.make = function (view) { @@ -139,27 +139,23 @@ if (name in this._cache) { value = this._cache[name]; } else { - if (name === '.') { - value = this.view; - } else { - var context = this; + var context = this; - while (context) { - if (name.indexOf('.') > 0) { - value = context.view; + while (context) { + if (name.indexOf('.') > 0) { + value = context.view; - var names = name.split('.'), i = 0; - while (value != null && i < names.length) { - value = value[names[i++]]; - } - } else { - value = context.view[name]; + var names = name.split('.'), i = 0; + while (value != null && i < names.length) { + value = value[names[i++]]; } + } else { + value = context.view[name]; + } - if (value != null) break; + if (value != null) break; - context = context.parent; - } + context = context.parent; } this._cache[name] = value;