Skip to content

Commit f95b676

Browse files
authored
return early if possible (#86)
1 parent efe1547 commit f95b676

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ Since [Hermes doesn't support the `Intl.Segmenter` API](https://github.com/faceb
220220

221221
| Name | Unicode® | ESM? | Size | Size (min) | Size (min+gzip) | Size (min+br) | Size (min+zstd) |
222222
|------------------------------|----------|------|----------:|-----------:|----------------:|--------------:|----------------:|
223-
| `unicode-segmenter/grapheme` | 16.0.0 | ✔️ | 15,941 | 12,192 | 5,064 | 3,739 | 4,747 |
223+
| `unicode-segmenter/grapheme` | 16.0.0 | ✔️ | 15,921 | 12,189 | 5,060 | 3,773 | 4,746 |
224224
| `graphemer` | 15.0.0 | ✖️ ️| 410,435 | 95,104 | 15,752 | 10,660 | 15,911 |
225225
| `grapheme-splitter` | 10.0.0 | ✖️ | 122,252 | 23,680 | 7,852 | 4,841 | 6,750 |
226226
| `@formatjs/intl-segmenter`* | 15.0.0 | ✖️ | 603,285 | 369,560 | 72,218 | 49,416 | 67,975 |
@@ -236,7 +236,7 @@ Since [Hermes doesn't support the `Intl.Segmenter` API](https://github.com/faceb
236236

237237
| Name | Bytecode size | Bytecode size (gzip)* |
238238
|------------------------------|--------------:|----------------------:|
239-
| `unicode-segmenter/grapheme` | 20,925 | 11,013 |
239+
| `unicode-segmenter/grapheme` | 20,928 | 10,973 |
240240
| `graphemer` | 133,978 | 31,713 |
241241
| `grapheme-splitter` | 63,835 | 19,137 |
242242

src/grapheme.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,7 @@ export function* graphemeSegments(input) {
103103
catBegin = catBefore;
104104
}
105105

106-
if (cp >= 2325) {
107-
// Note: Lazily update `consonant` and `linker` state
108-
// which is a extra overhead only for Hindi text.
109-
if (!consonant && catBefore === 0) {
110-
consonant = isIndicConjunctConsonant(cp);
111-
} else if (catBefore === 3 /* Extend */) {
112-
// Note: \p{InCB=Linker} is a subset of \p{Extend}
113-
linker = isIndicConjunctLinker(cp);
114-
}
115-
}
116-
117-
if (cursor < len) {
118-
cp = /** @type {number} */ (input.codePointAt(cursor));
119-
catAfter = cat(cp, cache);
120-
} else {
106+
if (cursor >= len) {
121107
yield {
122108
segment: input.slice(index, cursor),
123109
index,
@@ -129,6 +115,20 @@ export function* graphemeSegments(input) {
129115
return;
130116
}
131117

118+
// Note: Lazily update `consonant` and `linker` state
119+
// which is a extra overhead only for Hindi text.
120+
if (cp >= 2325) {
121+
if (!consonant && catBefore === 0) {
122+
consonant = isIndicConjunctConsonant(cp);
123+
} else if (catBefore === 3 /* Extend */) {
124+
// Note: \p{InCB=Linker} is a subset of \p{Extend}
125+
linker = isIndicConjunctLinker(cp);
126+
}
127+
}
128+
129+
cp = /** @type {number} */ (input.codePointAt(cursor));
130+
catAfter = cat(cp, cache);
131+
132132
if (catBefore === 10 /* Regional_Indicator */) {
133133
risCount += 1;
134134
} else {

0 commit comments

Comments
 (0)