Skip to content

Commit

Permalink
perf: do not use u flag and cache symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Jun 12, 2024
1 parent 302170c commit 3a4f4a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"root": true,
"extends": ["plugin:grules/all"],
"rules": {
"no-await-in-loop": "off"
"no-await-in-loop": "off",
"require-unicode-regexp": "off"
}
}
24 changes: 11 additions & 13 deletions src/html.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
const arrayIsArray = Array.isArray;

const symbolIterator = Symbol.iterator;

const symbolAsyncIterator = Symbol.asyncIterator;

const escapeDictionary = {
'"': """,
"&": "&",
Expand All @@ -7,10 +13,7 @@ const escapeDictionary = {
"`": "`",
};

const escapeRegExp = new RegExp(
`[${Object.keys(escapeDictionary).join("")}]`,
"u",
);
const escapeRegExp = new RegExp(`[${Object.keys(escapeDictionary).join("")}]`);

const escapeFunction = (string) => {
const stringLength = string.length;
Expand All @@ -30,8 +33,6 @@ const escapeFunction = (string) => {
return escaped + string.slice(start, end);
};

const arrayIsArray = Array.isArray;

/**
* @param {{ raw: string[] }} literals Tagged template literals.
* @param {...any} expressions Expressions to interpolate.
Expand Down Expand Up @@ -85,7 +86,7 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
} else if (expression === undefined || expression === null) {
string = "";
} else {
if (expression[Symbol.iterator]) {
if (expression[symbolIterator]) {
const isRaw =
literal !== "" && literal.charCodeAt(literal.length - 1) === 33;

Expand All @@ -105,7 +106,7 @@ const htmlGenerator = function* ({ raw: literals }, ...expressions) {
continue;
}

if (expression[Symbol.iterator]) {
if (expression[symbolIterator]) {
for (expression of expression) {
if (typeof expression === "string") {
string = expression;
Expand Down Expand Up @@ -182,7 +183,7 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
} else if (expression === undefined || expression === null) {
string = "";
} else {
if (expression[Symbol.iterator] || expression[Symbol.asyncIterator]) {
if (expression[symbolIterator] || expression[symbolAsyncIterator]) {
const isRaw =
literal !== "" && literal.charCodeAt(literal.length - 1) === 33;

Expand All @@ -202,10 +203,7 @@ const htmlAsyncGenerator = async function* ({ raw: literals }, ...expressions) {
continue;
}

if (
expression[Symbol.iterator] ||
expression[Symbol.asyncIterator]
) {
if (expression[symbolIterator] || expression[symbolAsyncIterator]) {
for await (expression of expression) {
if (typeof expression === "string") {
string = expression;
Expand Down

0 comments on commit 3a4f4a5

Please sign in to comment.