Skip to content

Commit

Permalink
perf: simplify loops
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Jul 17, 2024
1 parent 793bd87 commit 5a67304
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/html.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const escapeRegExp = /["&'<>`]/;

Check warning on line 1 in src/html.js

View workflow job for this annotation

GitHub Actions / test (^18)

Use the 'u' flag

Check warning on line 1 in src/html.js

View workflow job for this annotation

GitHub Actions / test (lts/*)

Use the 'u' flag

const escapeFunction = (string) => {
const stringLength = string.length;
let start = 0;
let escaped = "";
let start = 0;
let end = 0;

for (let end = 0; end !== stringLength; ++end) {
for (; end !== string.length; ++end) {
switch (string.charCodeAt(end)) {
case 34: // "
escaped += string.slice(start, end) + "&#34;";
Expand Down Expand Up @@ -34,7 +34,7 @@ const escapeFunction = (string) => {
}
}

escaped += string.slice(start, stringLength);
escaped += string.slice(start, end);

return escaped;
};
Expand All @@ -45,10 +45,10 @@ const escapeFunction = (string) => {
* @returns {string} The HTML string.
*/
const html = ({ raw: literals }, ...expressions) => {
const expressionsLength = expressions.length;
let accumulator = "";
let index = 0;

for (let index = 0; index !== expressionsLength; ++index) {
for (; index !== expressions.length; ++index) {
const expression = expressions[index];
let literal = literals[index];
let string =
Expand All @@ -69,7 +69,7 @@ const html = ({ raw: literals }, ...expressions) => {
accumulator += literal + string;
}

accumulator += literals[expressionsLength];
accumulator += literals[index];

return accumulator;
};
Expand All @@ -80,9 +80,9 @@ const html = ({ raw: literals }, ...expressions) => {
* @yields {string} The HTML strings.
*/
const htmlGenerator = function* ({ raw: literals }, ...expressions) {
const expressionsLength = expressions.length;
let index = 0;

for (let index = 0; index !== expressionsLength; ++index) {
for (; index !== expressions.length; ++index) {
let expression = expressions[index];
let literal = literals[index];
let string;
Expand Down Expand Up @@ -165,8 +165,8 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
}
}

if (literals[expressionsLength]) {
yield literals[expressionsLength];
if (literals[index]) {
yield literals[index];
}
};

Expand All @@ -176,9 +176,9 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
* @yields {string} The HTML strings.
*/
const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
const expressionsLength = expressions.length;
let index = 0;

for (let index = 0; index !== expressionsLength; ++index) {
for (; index !== expressions.length; ++index) {
let expression = await expressions[index];

Check warning on line 182 in src/html.js

View workflow job for this annotation

GitHub Actions / test (^18)

Unexpected `await` inside a loop

Check warning on line 182 in src/html.js

View workflow job for this annotation

GitHub Actions / test (lts/*)

Unexpected `await` inside a loop
let literal = literals[index];
let string;
Expand Down Expand Up @@ -264,8 +264,8 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
}
}

if (literals[expressionsLength]) {
yield literals[expressionsLength];
if (literals[index]) {
yield literals[index];
}
};

Expand Down

0 comments on commit 5a67304

Please sign in to comment.