Skip to content

Commit 3027e90

Browse files
committed
slight performance improvement by caching several variable
1 parent 903600d commit 3027e90

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/integrations/misc/extract-text.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -289,18 +289,21 @@ export function processCarriageReturns(input: string): string {
289289
// Partial overwrite - need to check for multi-byte character boundary issues
290290
const potentialPartialChar = curLine.charAt(segment.length)
291291

292+
// Cache character code points to avoid repeated charCodeAt calls
293+
const hasPartialChar = potentialPartialChar !== ""
294+
const segmentLastCharCode = segment.length > 0 ? segment.charCodeAt(segment.length - 1) : 0
295+
const partialCharCode = hasPartialChar ? potentialPartialChar.charCodeAt(0) : 0
296+
292297
// Check if character is part of a multi-byte sequence (emoji or other Unicode characters)
293298
// Detect surrogate pairs (high/low surrogates) to identify multi-byte characters
294299
if (
295-
potentialPartialChar &&
300+
hasPartialChar &&
296301
((segment.length > 0 &&
297-
((segment.charCodeAt(segment.length - 1) >= 0xd800 &&
298-
segment.charCodeAt(segment.length - 1) <= 0xdbff) ||
299-
(potentialPartialChar.charCodeAt(0) >= 0xdc00 &&
300-
potentialPartialChar.charCodeAt(0) <= 0xdfff))) ||
302+
((segmentLastCharCode >= 0xd800 && segmentLastCharCode <= 0xdbff) ||
303+
(partialCharCode >= 0xdc00 && partialCharCode <= 0xdfff))) ||
301304
(curLine.length > segment.length + 1 &&
302-
potentialPartialChar.charCodeAt(0) >= 0xd800 &&
303-
potentialPartialChar.charCodeAt(0) <= 0xdbff))
305+
partialCharCode >= 0xd800 &&
306+
partialCharCode <= 0xdbff))
304307
) {
305308
// If a partially overwritten multi-byte character is detected, replace with space
306309
const remainPart = curLine.substring(segment.length + 1)

0 commit comments

Comments
 (0)