Skip to content

Commit fb41732

Browse files
authored
perf: cache str length
Signed-off-by: francesco <[email protected]>
1 parent af09501 commit fb41732

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

index.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -141,31 +141,34 @@ function inlineAsString (input) {
141141
function inlineAsStringInternal (input) {
142142
return `
143143
// #region inlineAsStringInternal
144-
if (${input}.length === 0) {
145-
json += JSON_STR_EMPTY_STRING
146-
} else if (${input}.length < 42) {
147-
let result = ''
148-
let last = -1
149-
let point = 255
150-
for (let i = 0; i < ${input}.length; i++) {
151-
point = ${input}.charCodeAt(i)
152-
if (point === 0x22 || point === 0x5c) {
153-
last === -1 && (last = 0)
154-
result += ${input}.slice(last, i) + '\\\\'
155-
last = i
156-
} else if (point < 32 || (point >= 0xD800 && point <= 0xDFFF)) {
157-
json += JSON.stringify(${input})
158-
result = null
159-
break
144+
{
145+
const len = ${input}.length
146+
if (len === 0) {
147+
json += JSON_STR_EMPTY_STRING
148+
} else if (len < 42) {
149+
let result = ''
150+
let last = -1
151+
let point = 255
152+
for (let i = 0; i < len; i++) {
153+
point = ${input}.charCodeAt(i)
154+
if (point === 0x22 || point === 0x5c) {
155+
last === -1 && (last = 0)
156+
result += ${input}.slice(last, i) + '\\\\'
157+
last = i
158+
} else if (point < 32 || (point >= 0xD800 && point <= 0xDFFF)) {
159+
json += JSON.stringify(${input})
160+
result = null
161+
break
162+
}
160163
}
164+
if (result !== null) {
165+
json += JSON_STR_QUOTE + (last === -1 ? ${input} : (result + ${input}.slice(last))) + JSON_STR_QUOTE
166+
}
167+
} else if (len < 5000 && STR_ESCAPE.test(${input}) === false) {
168+
json += JSON_STR_QUOTE + ${input} + JSON_STR_QUOTE
169+
} else {
170+
json += JSON.stringify(${input})
161171
}
162-
if (result !== null) {
163-
json += JSON_STR_QUOTE + (last === -1 ? ${input} : (result + ${input}.slice(last))) + JSON_STR_QUOTE
164-
}
165-
} else if (${input}.length < 5000 && STR_ESCAPE.test(${input}) === false) {
166-
json += JSON_STR_QUOTE + ${input} + JSON_STR_QUOTE
167-
} else {
168-
json += JSON.stringify(${input})
169172
}
170173
// #endregion inlineAsStringInternal
171174
`

0 commit comments

Comments
 (0)