From caadd5bb32adad4702ffa97c592a7554642a5aab Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 11 Jul 2014 00:23:06 -0700 Subject: [PATCH] Optimize `_.escape`, `_.escapeRegExp`, and `_.unescape`. --- lodash.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lodash.js b/lodash.js index a635586ec..7744b2185 100644 --- a/lodash.js +++ b/lodash.js @@ -7792,7 +7792,10 @@ * // => 'fred, barney, & pebbles' */ function escape(string) { - return string == null ? '' : String(string).replace(reUnescapedHtml, escapeHtmlChar); + string = string == null ? '' : String(string); + return (reUnescapedHtml.lastIndex = 0, reUnescapedHtml.test(string)) + ? string.replace(reUnescapedHtml, escapeHtmlChar) + : string; } /** @@ -7810,7 +7813,10 @@ * // => '\[lodash\]\(http://lodash\.com\)' */ function escapeRegExp(string) { - return string == null ? '' : String(string).replace(reRegExpChars, '\\$&'); + string = string == null ? '' : String(string); + return (reRegExpChars.lastIndex = 0, reRegExpChars.test(string)) + ? string.replace(reRegExpChars, '\\$&') + : string; } /** @@ -8401,11 +8407,10 @@ * // => 'fred, barney & pebbles' */ function unescape(string) { - if (string == null) { - return ''; - } - string = String(string); - return string.indexOf(';') < 0 ? string : string.replace(reEscapedHtml, unescapeHtmlChar); + string = string == null ? '' : String(string); + return (reEscapedHtml.lastIndex = 0, reEscapedHtml.test(string)) + ? string.replace(reEscapedHtml, unescapeHtmlChar) + : string; } /*--------------------------------------------------------------------------*/