Skip to content

Commit 74da2c4

Browse files
BridgeARMylesBorins
authored andcommitted
util: improve getStringWidth performance
This makes sure the common path does not normalize the input string. It's only required for characters that are outside of the ASCII range. Signed-off-by: Ruben Bridgewater <[email protected]> PR-URL: #33674 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Anto Aravinth <[email protected]>
1 parent 39894f8 commit 74da2c4

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

lib/internal/util/inspect.js

+6-10
Original file line numberDiff line numberDiff line change
@@ -1995,13 +1995,6 @@ function formatWithOptionsInternal(inspectOptions, ...args) {
19951995
return str;
19961996
}
19971997

1998-
function prepareStringForGetStringWidth(str, removeControlChars) {
1999-
str = str.normalize('NFC');
2000-
if (removeControlChars)
2001-
str = stripVTControlCharacters(str);
2002-
return str;
2003-
}
2004-
20051998
if (internalBinding('config').hasIntl) {
20061999
const icu = internalBinding('icu');
20072000
// icu.getStringWidth(string, ambiguousAsFullWidth, expandEmojiSequence)
@@ -2012,13 +2005,14 @@ if (internalBinding('config').hasIntl) {
20122005
getStringWidth = function getStringWidth(str, removeControlChars = true) {
20132006
let width = 0;
20142007

2015-
str = prepareStringForGetStringWidth(str, removeControlChars);
2008+
if (removeControlChars)
2009+
str = stripVTControlCharacters(str);
20162010
for (let i = 0; i < str.length; i++) {
20172011
// Try to avoid calling into C++ by first handling the ASCII portion of
20182012
// the string. If it is fully ASCII, we skip the C++ part.
20192013
const code = str.charCodeAt(i);
20202014
if (code >= 127) {
2021-
width += icu.getStringWidth(str.slice(i));
2015+
width += icu.getStringWidth(str.slice(i).normalize('NFC'));
20222016
break;
20232017
}
20242018
width += code >= 32 ? 1 : 0;
@@ -2032,7 +2026,9 @@ if (internalBinding('config').hasIntl) {
20322026
getStringWidth = function getStringWidth(str, removeControlChars = true) {
20332027
let width = 0;
20342028

2035-
str = prepareStringForGetStringWidth(str, removeControlChars);
2029+
if (removeControlChars)
2030+
str = stripVTControlCharacters(str);
2031+
str = str.normalize('NFC');
20362032
for (const char of str) {
20372033
const code = char.codePointAt(0);
20382034
if (isFullWidthCodePoint(code)) {

0 commit comments

Comments
 (0)