Skip to content

Commit 58cd76c

Browse files
BridgeARaddaleax
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 39dea8f commit 58cd76c

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
@@ -1953,13 +1953,6 @@ function formatWithOptions(inspectOptions, ...args) {
19531953
return str;
19541954
}
19551955

1956-
function prepareStringForGetStringWidth(str, removeControlChars) {
1957-
str = str.normalize('NFC');
1958-
if (removeControlChars)
1959-
str = stripVTControlCharacters(str);
1960-
return str;
1961-
}
1962-
19631956
if (internalBinding('config').hasIntl) {
19641957
const icu = internalBinding('icu');
19651958
// icu.getStringWidth(string, ambiguousAsFullWidth, expandEmojiSequence)
@@ -1970,13 +1963,14 @@ if (internalBinding('config').hasIntl) {
19701963
getStringWidth = function getStringWidth(str, removeControlChars = true) {
19711964
let width = 0;
19721965

1973-
str = prepareStringForGetStringWidth(str, removeControlChars);
1966+
if (removeControlChars)
1967+
str = stripVTControlCharacters(str);
19741968
for (let i = 0; i < str.length; i++) {
19751969
// Try to avoid calling into C++ by first handling the ASCII portion of
19761970
// the string. If it is fully ASCII, we skip the C++ part.
19771971
const code = str.charCodeAt(i);
19781972
if (code >= 127) {
1979-
width += icu.getStringWidth(str.slice(i));
1973+
width += icu.getStringWidth(str.slice(i).normalize('NFC'));
19801974
break;
19811975
}
19821976
width += code >= 32 ? 1 : 0;
@@ -1990,7 +1984,9 @@ if (internalBinding('config').hasIntl) {
19901984
getStringWidth = function getStringWidth(str, removeControlChars = true) {
19911985
let width = 0;
19921986

1993-
str = prepareStringForGetStringWidth(str, removeControlChars);
1987+
if (removeControlChars)
1988+
str = stripVTControlCharacters(str);
1989+
str = str.normalize('NFC');
19941990
for (const char of str) {
19951991
const code = char.codePointAt(0);
19961992
if (isFullWidthCodePoint(code)) {

0 commit comments

Comments
 (0)