From dce08b7fcfa31e101da4936ee62ee6402631cf5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Fri, 18 Oct 2024 21:19:30 +0200 Subject: [PATCH] perf: cache static methods --- src/index.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 513ec3a..21df0d5 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,8 @@ +const _isArray = Array.isArray; + +const _iterator = Symbol.iterator; +const _asyncIterator = Symbol.asyncIterator; + const escapeRegExp = /["&'<=>]/g; const escapeFunction = (string) => { @@ -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] ?? ""}`; @@ -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; @@ -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; @@ -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; @@ -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) {