Skip to content

Commit

Permalink
perf: use === for string length comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Oct 18, 2024
1 parent 6ae5d88 commit bd8383d
Showing 1 changed file with 29 additions and 26 deletions.
55 changes: 29 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ export const html = (literals, ...expressions) => {
? expressions[i].join("")
: `${expressions[i] ?? ""}`;

if (literal && literal.charCodeAt(literal.length - 1) === 33) {
if (literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33) {
literal = literal.slice(0, -1);
} else {
string &&= escapeFunction(string);
} else if (string.length !== 0) {
string = escapeFunction(string);
}

accumulator += literal + string;
Expand All @@ -89,15 +89,15 @@ export const htmlGenerator = function* (literals, ...expressions) {
} else if (expression === undefined || expression === null) {
string = "";
} else {
if (expression[Symbol.iterator]) {
if (typeof expression[Symbol.iterator] === "function") {
const isRaw =
Boolean(literal) && literal.charCodeAt(literal.length - 1) === 33;
literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33;

if (isRaw) {
literal = literal.slice(0, -1);
}

if (literal) {
if (literal.length !== 0) {
yield literal;
}

Expand All @@ -109,15 +109,15 @@ export const htmlGenerator = function* (literals, ...expressions) {
continue;
}

if (expression[Symbol.iterator]) {
if (typeof expression[Symbol.iterator] === "function") {
for (expression of expression) {
if (expression === undefined || expression === null) {
continue;
}

string = String(expression);

if (string) {
if (string.length !== 0) {
if (!isRaw) {
string = escapeFunction(string);
}
Expand All @@ -132,7 +132,7 @@ export const htmlGenerator = function* (literals, ...expressions) {
string = String(expression);
}

if (string) {
if (string.length !== 0) {
if (!isRaw) {
string = escapeFunction(string);
}
Expand All @@ -147,18 +147,18 @@ export const htmlGenerator = function* (literals, ...expressions) {
string = String(expression);
}

if (literal && literal.charCodeAt(literal.length - 1) === 33) {
if (literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33) {
literal = literal.slice(0, -1);
} else {
string &&= escapeFunction(string);
} else if (string.length !== 0) {
string = escapeFunction(string);
}

if (literal || string) {
if (literal.length !== 0 || string.length !== 0) {
yield literal + string;
}
}

if (literals.raw[expressions.length]) {
if (literals.raw[expressions.length].length !== 0) {
yield literals.raw[expressions.length];
}
};
Expand All @@ -180,15 +180,18 @@ export const htmlAsyncGenerator = async function* (literals, ...expressions) {
} else if (expression === undefined || expression === null) {
string = "";
} else {
if (expression[Symbol.iterator] || expression[Symbol.asyncIterator]) {
if (
typeof expression[Symbol.iterator] === "function" ||
typeof expression[Symbol.asyncIterator] === "function"
) {
const isRaw =
Boolean(literal) && literal.charCodeAt(literal.length - 1) === 33;
literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33;

if (isRaw) {
literal = literal.slice(0, -1);
}

if (literal) {
if (literal.length !== 0) {
yield literal;
}

Expand All @@ -201,8 +204,8 @@ export const htmlAsyncGenerator = async function* (literals, ...expressions) {
}

if (
expression[Symbol.iterator] ||
expression[Symbol.asyncIterator]
typeof expression[Symbol.iterator] === "function" ||
typeof expression[Symbol.asyncIterator] === "function"
) {
for await (expression of expression) {
if (expression === undefined || expression === null) {
Expand All @@ -211,7 +214,7 @@ export const htmlAsyncGenerator = async function* (literals, ...expressions) {

string = String(expression);

if (string) {
if (string.length !== 0) {
if (!isRaw) {
string = escapeFunction(string);
}
Expand All @@ -226,7 +229,7 @@ export const htmlAsyncGenerator = async function* (literals, ...expressions) {
string = String(expression);
}

if (string) {
if (string.length !== 0) {
if (!isRaw) {
string = escapeFunction(string);
}
Expand All @@ -241,18 +244,18 @@ export const htmlAsyncGenerator = async function* (literals, ...expressions) {
string = String(expression);
}

if (literal && literal.charCodeAt(literal.length - 1) === 33) {
if (literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33) {
literal = literal.slice(0, -1);
} else {
string &&= escapeFunction(string);
} else if (string.length !== 0) {
string = escapeFunction(string);
}

if (literal || string) {
if (literal.length !== 0 || string.length !== 0) {
yield literal + string;
}
}

if (literals.raw[expressions.length]) {
if (literals.raw[expressions.length].length !== 0) {
yield literals.raw[expressions.length];
}
};

0 comments on commit bd8383d

Please sign in to comment.