Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 58cd76c

Browse filesBrowse 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 <ruben@bridgewater.de> PR-URL: #33674 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
1 parent 39dea8f commit 58cd76c
Copy full SHA for 58cd76c

File tree

Expand file treeCollapse file tree

1 file changed

+6
-10
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+6
-10
lines changed
Open diff view settings
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+6-10Lines changed: 6 additions & 10 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.