Skip to content

Commit a8dbdb4

Browse files
committed
feat: reduce slicing
1 parent 8ac0ecf commit a8dbdb4

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"c8": "^10.1.2",
3030
"globals": "^16.0.0",
3131
"grules": "^0.26.1",
32-
"tinybench": "^4.0.1",
32+
"tinybench": "^5.0.1",
3333
"typescript": ">=5.7.2"
3434
},
3535
"repository": {

src/index.js

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
const escapeRegExp = /["&'<=>]/g;
44

55
const escapeFunction = (string) => {
6+
if (!escapeRegExp.test(string)) {
7+
return string;
8+
}
9+
610
let escaped = "";
711
let start = 0;
812

9-
while (escapeRegExp.test(string)) {
13+
do {
1014
const i = escapeRegExp.lastIndex - 1;
1115

1216
switch (string.charCodeAt(i)) {
@@ -43,7 +47,7 @@ const escapeFunction = (string) => {
4347
}
4448

4549
start = escapeRegExp.lastIndex;
46-
}
50+
} while (escapeRegExp.test(string));
4751

4852
return escaped + string.slice(start);
4953
};
@@ -55,8 +59,9 @@ const escapeFunction = (string) => {
5559
*/
5660
const html = (literals, ...expressions) => {
5761
let accumulator = "";
62+
const expressionsLength = expressions.length;
5863

59-
for (let i = 0; i !== expressions.length; ++i) {
64+
for (let i = 0; i < expressionsLength; ++i) {
6065
let literal = literals.raw[i];
6166
let string =
6267
typeof expressions[i] === "string"
@@ -76,7 +81,7 @@ const html = (literals, ...expressions) => {
7681
accumulator += literal + string;
7782
}
7883

79-
return accumulator + literals.raw[expressions.length];
84+
return accumulator + literals.raw[expressionsLength];
8085
};
8186

8287
/**
@@ -86,7 +91,9 @@ const html = (literals, ...expressions) => {
8691
* @returns {Generator<string, void, void>} Generator<string, void, void>
8792
*/
8893
const htmlGenerator = function* (literals, ...expressions) {
89-
for (let i = 0; i !== expressions.length; ++i) {
94+
const expressionsLength = expressions.length;
95+
96+
for (let i = 0; i < expressionsLength; ++i) {
9097
let expression = expressions[i];
9198
let literal = literals.raw[i];
9299
let string;
@@ -165,8 +172,8 @@ const htmlGenerator = function* (literals, ...expressions) {
165172
}
166173
}
167174

168-
if (literals.raw[expressions.length].length) {
169-
yield literals.raw[expressions.length];
175+
if (literals.raw[expressionsLength].length) {
176+
yield literals.raw[expressionsLength];
170177
}
171178
};
172179

@@ -177,7 +184,9 @@ const htmlGenerator = function* (literals, ...expressions) {
177184
* @returns {AsyncGenerator<string, void, void>} AsyncGenerator<string, void, void>
178185
*/
179186
const htmlAsyncGenerator = async function* (literals, ...expressions) {
180-
for (let i = 0; i !== expressions.length; ++i) {
187+
const expressionsLength = expressions.length;
188+
189+
for (let i = 0; i < expressionsLength; ++i) {
181190
let expression = await expressions[i];
182191
let literal = literals.raw[i];
183192
let string;
@@ -262,8 +271,8 @@ const htmlAsyncGenerator = async function* (literals, ...expressions) {
262271
}
263272
}
264273

265-
if (literals.raw[expressions.length].length) {
266-
yield literals.raw[expressions.length];
274+
if (literals.raw[expressionsLength].length) {
275+
yield literals.raw[expressionsLength];
267276
}
268277
};
269278

0 commit comments

Comments
 (0)