Skip to content

Commit

Permalink
perf: cache static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Oct 18, 2024
1 parent bd8383d commit dce08b7
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
const _isArray = Array.isArray;

const _iterator = Symbol.iterator;
const _asyncIterator = Symbol.asyncIterator;

const escapeRegExp = /["&'<=>]/g;

const escapeFunction = (string) => {
Expand Down Expand Up @@ -56,7 +61,7 @@ export const html = (literals, ...expressions) => {

for (let i = 0; i !== expressions.length; ++i) {
let literal = literals.raw[i];
let string = Array.isArray(expressions[i])
let string = _isArray(expressions[i])
? expressions[i].join("")
: `${expressions[i] ?? ""}`;

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

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

if (typeof expression[Symbol.iterator] === "function") {
if (typeof expression[_iterator] === "function") {
for (expression of expression) {
if (expression === undefined || expression === null) {
continue;
Expand Down Expand Up @@ -181,8 +186,8 @@ export const htmlAsyncGenerator = async function* (literals, ...expressions) {
string = "";
} else {
if (
typeof expression[Symbol.iterator] === "function" ||
typeof expression[Symbol.asyncIterator] === "function"
typeof expression[_iterator] === "function" ||
typeof expression[_asyncIterator] === "function"
) {
const isRaw =
literal.length !== 0 && literal.charCodeAt(literal.length - 1) === 33;
Expand All @@ -204,8 +209,8 @@ export const htmlAsyncGenerator = async function* (literals, ...expressions) {
}

if (
typeof expression[Symbol.iterator] === "function" ||
typeof expression[Symbol.asyncIterator] === "function"
typeof expression[_iterator] === "function" ||
typeof expression[_asyncIterator] === "function"
) {
for await (expression of expression) {
if (expression === undefined || expression === null) {
Expand Down

0 comments on commit dce08b7

Please sign in to comment.