From cfc6c8f80eddb465356940e71409f5e5740686bd Mon Sep 17 00:00:00 2001 From: Sahab Yazdani Date: Mon, 29 Nov 2010 19:22:46 -0800 Subject: [PATCH] Speed up interpreted mode by saving the compiled regex in escape_regex. --- mustache.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/mustache.js b/mustache.js index ca5b589..29a005a 100644 --- a/mustache.js +++ b/mustache.js @@ -11,6 +11,8 @@ var Mustache = function() { ParserException.prototype = {}; var Renderer = function(send_func, mode) { + this._escapeCompiledRegex = null; + this.user_send_func = send_func; if (mode==='interpreter' || !mode) { this.commandSet = this.interpreter; @@ -239,13 +241,15 @@ var Mustache = function() { escape_regex: function(text) { // thank you Simon Willison - var specials = [ - '/', '.', '*', '+', '?', '|', - '(', ')', '[', ']', '{', '}', '\\' - ]; - var compiled_regex = new RegExp('(\\' + specials.join('|\\') + ')', 'g'); + if (!this._escapeCompiledRegex) { + var specials = [ + '/', '.', '*', '+', '?', '|', + '(', ')', '[', ']', '{', '}', '\\' + ]; + this._escapeCompiledRegex = new RegExp('(\\' + specials.join('|\\') + ')', 'g'); + } - return text.replace(compiled_regex, '\\$1'); + return text.replace(this._escapeCompiledRegex, '\\$1'); }, isWhitespace: function(token) {