Skip to content

Commit 71b4dd2

Browse files
committed
Reduce count operations
1 parent 27b88b3 commit 71b4dd2

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

library/src/utils/_getCodePointCount/_getCodePointCountCount.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
* @internal
99
*/
1010
export function _getCodePointCount(input: string): number {
11-
let count = 0;
12-
for (let i = 0; i < input.length; count++) {
11+
let count = input.length;
12+
for (let i = 0; i < input.length; ) {
1313
// If codePoint were undefined here, we would have already got out of loop
1414
if (input.codePointAt(i)! <= 65535) {
1515
i++;
1616
} else {
1717
i += 2; // 2 characters (surrogate pair) in JS (UTF-16)
18+
count--; // compensate for over-counting
1819
}
1920
}
2021
return count;

0 commit comments

Comments
 (0)