Optimize hot path in escapeExpression

Avoid deoptimizations in v8 due to the duct type check on string instances.

Partial fix for #973
This commit is contained in:
kpdecker
2015-03-16 22:06:01 -05:00
parent 64ab232d72
commit ab96073c6b
+15 -13
View File
@@ -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);
}