Skip to content

Commit b0dd0fa

Browse files
authoredJan 22, 2024
Improve performance (#54)
1 parent 210a997 commit b0dd0fa

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed
 

Diff for: ‎index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import stripAnsi from 'strip-ansi';
22
import {eastAsianWidth} from 'get-east-asian-width';
33
import emojiRegex from 'emoji-regex';
44

5+
const segmenter = new Intl.Segmenter();
6+
57
export default function stringWidth(string, options = {}) {
68
if (typeof string !== 'string' || string.length === 0) {
79
return 0;
@@ -21,8 +23,9 @@ export default function stringWidth(string, options = {}) {
2123
}
2224

2325
let width = 0;
26+
const eastAsianWidthOptions = {ambiguousAsWide: !ambiguousIsNarrow};
2427

25-
for (const {segment: character} of new Intl.Segmenter().segment(string)) {
28+
for (const {segment: character} of segmenter.segment(string)) {
2629
const codePoint = character.codePointAt(0);
2730

2831
// Ignore control characters
@@ -40,7 +43,7 @@ export default function stringWidth(string, options = {}) {
4043
continue;
4144
}
4245

43-
width += eastAsianWidth(codePoint, {ambiguousAsWide: !ambiguousIsNarrow});
46+
width += eastAsianWidth(codePoint, eastAsianWidthOptions);
4447
}
4548

4649
return width;

0 commit comments

Comments
 (0)
Please sign in to comment.