summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorkpdecker <kpdecker@gmail.com>2015-03-16 22:06:01 -0500
committerkpdecker <kpdecker@gmail.com>2015-03-16 22:06:01 -0500
commitab96073c6bfde771dde1e8ff1aa7444192f7f6df (patch)
tree6a05e56d80e97f6788e01775ac8ac54643f2390f /lib
parent64ab232d723f5935e21b0e470d04dcf369986172 (diff)
downloadhandlebars.js-ab96073c6bfde771dde1e8ff1aa7444192f7f6df.zip
handlebars.js-ab96073c6bfde771dde1e8ff1aa7444192f7f6df.tar.gz
handlebars.js-ab96073c6bfde771dde1e8ff1aa7444192f7f6df.tar.bz2
Optimize hot path in escapeExpression
Avoid deoptimizations in v8 due to the duct type check on string instances. Partial fix for #973
Diffstat (limited to 'lib')
-rw-r--r--lib/handlebars/utils.js28
1 files changed, 15 insertions, 13 deletions
diff --git a/lib/handlebars/utils.js b/lib/handlebars/utils.js
index fc2b846..41495e1 100644
--- a/lib/handlebars/utils.js
+++ b/lib/handlebars/utils.js
@@ -60,21 +60,23 @@ export function indexOf(array, value) {
export function escapeExpression(string) {
- // don't escape SafeStrings, since they're already safe
- if (string && string.toHTML) {
- return string.toHTML();
- } else if (string == null) {
- return "";
- } else if (!string) {
- return string + '';
- }
+ if (typeof string !== 'string') {
+ // don't escape SafeStrings, since they're already safe
+ if (string && string.toHTML) {
+ return string.toHTML();
+ } else if (string == null) {
+ return '';
+ } else if (!string) {
+ return string + '';
+ }
- // Force a string conversion as this will be done by the append regardless and
- // the regex test will do this transparently behind the scenes, causing issues if
- // an object's to string has escaped characters in it.
- string = "" + string;
+ // Force a string conversion as this will be done by the append regardless and
+ // the regex test will do this transparently behind the scenes, causing issues if
+ // an object's to string has escaped characters in it.
+ string = '' + string;
+ }
- if(!possible.test(string)) { return string; }
+ if (!possible.test(string)) { return string; }
return string.replace(badChars, escapeChar);
}