Skip to content

Commit

Permalink
Optimize hot path in escapeExpression
Browse files Browse the repository at this point in the history
Avoid deoptimizations in v8 due to the duct type check on string instances.

Partial fix for #973
  • Loading branch information
kpdecker committed Mar 17, 2015
1 parent 64ab232 commit ab96073
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/handlebars/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit ab96073

Please sign in to comment.