Skip to content

Commit

Permalink
perf: speed up RegExp with character classes
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasvantuycom committed Sep 28, 2023
1 parent 6224a39 commit 35e2943
Showing 1 changed file with 1 addition and 9 deletions.
10 changes: 1 addition & 9 deletions lib/replace.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,7 @@ import escapeStringRegexp from 'escape-string-regexp';
export default function replace(contents, manifest) {
let newContents = contents;
for (const [originalPath, revisionedPath] of Object.entries(manifest)) {
const FRONT_DELIMITERS = ['"', '\'', '\\s', ',', '\\(', '\\/', '=', '^', '%2F'];
const BACK_DELIMITERS = ['"', '\'', '\\s', ',', '\\)', '\\\\', '\\?', '#', '$', '&', '>'];

const regexp = new RegExp(
`(?<=${FRONT_DELIMITERS.join('|')})${escapeStringRegexp(
originalPath,
)}(?=${BACK_DELIMITERS.join('|')})`,
'g',
);
const regexp = new RegExp(`(?<![\\w-])${escapeStringRegexp(originalPath)}(?![\\w.])`, 'g');

newContents = newContents.replace(regexp, revisionedPath);
}
Expand Down

0 comments on commit 35e2943

Please sign in to comment.