Skip to content

Commit

Permalink
perf(index): cache regex literals
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Oct 29, 2023
1 parent 21842b9 commit 7c1296e
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const errorMessages = {
3221226505: "Internal process error",
};

// Cache immutable regex as they are expensive to create and garbage collect
const popplerVersionRegex = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u;
const pdfInfoFileSizesRegex = /(File\s+size:\s+)0(\s+)bytes/u;

/**
* @author Frazer Smith
* @description Checks each option provided is valid, of the correct type, and can be used by specified
Expand Down Expand Up @@ -222,7 +226,7 @@ class Poppler {
);

// @ts-ignore: parseOptions checks if falsy
const versionInfo = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u.exec(stderr)[1];
const versionInfo = popplerVersionRegex.exec(stderr)[1];

const args = parseOptions(acceptedOptions, options, versionInfo);

Expand Down Expand Up @@ -327,7 +331,7 @@ class Poppler {
);

// @ts-ignore: parseOptions checks if falsy
const versionInfo = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u.exec(stderr)[1];
const versionInfo = popplerVersionRegex.exec(stderr)[1];

const args = parseOptions(acceptedOptions, options, versionInfo);

Expand Down Expand Up @@ -451,7 +455,7 @@ class Poppler {
);

// @ts-ignore: parseOptions checks if falsy
const versionInfo = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u.exec(stderr)[1];
const versionInfo = popplerVersionRegex.exec(stderr)[1];

const args = parseOptions(acceptedOptions, options, versionInfo);

Expand Down Expand Up @@ -496,7 +500,7 @@ class Poppler {
if (stdOut !== "") {
if (fileSize) {
stdOut = stdOut.replace(
/(File\s+size:\s+)0(\s+)bytes/u,
pdfInfoFileSizesRegex,
`$1${fileSize}$2bytes`
);
}
Expand Down Expand Up @@ -570,7 +574,7 @@ class Poppler {
);

// @ts-ignore: parseOptions checks if falsy
const versionInfo = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u.exec(stderr)[1];
const versionInfo = popplerVersionRegex.exec(stderr)[1];

const args = parseOptions(acceptedOptions, options, versionInfo);
args.push(file);
Expand Down Expand Up @@ -748,7 +752,7 @@ class Poppler {
);

// @ts-ignore: parseOptions checks if falsy
const versionInfo = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u.exec(stderr)[1];
const versionInfo = popplerVersionRegex.exec(stderr)[1];

const args = parseOptions(acceptedOptions, options, versionInfo);

Expand Down Expand Up @@ -896,7 +900,7 @@ class Poppler {
);

// @ts-ignore: parseOptions checks if falsy
const versionInfo = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u.exec(stderr)[1];
const versionInfo = popplerVersionRegex.exec(stderr)[1];

const args = parseOptions(acceptedOptions, options, versionInfo);

Expand Down Expand Up @@ -1101,7 +1105,7 @@ class Poppler {
);

// @ts-ignore: parseOptions checks if falsy
const versionInfo = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u.exec(stderr)[1];
const versionInfo = popplerVersionRegex.exec(stderr)[1];

const args = parseOptions(acceptedOptions, options, versionInfo);

Expand Down Expand Up @@ -1339,7 +1343,7 @@ class Poppler {
);

// @ts-ignore: parseOptions checks if falsy
const versionInfo = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u.exec(stderr)[1];
const versionInfo = popplerVersionRegex.exec(stderr)[1];

const args = parseOptions(acceptedOptions, options, versionInfo);

Expand Down Expand Up @@ -1496,7 +1500,7 @@ class Poppler {
);

// @ts-ignore: parseOptions checks if falsy
const versionInfo = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u.exec(stderr)[1];
const versionInfo = popplerVersionRegex.exec(stderr)[1];

const args = parseOptions(acceptedOptions, options, versionInfo);

Expand Down Expand Up @@ -1582,7 +1586,7 @@ class Poppler {
);

// @ts-ignore: parseOptions checks if falsy
const versionInfo = /(\d{1,2}\.\d{1,2}\.\d{1,2})/u.exec(stderr)[1];
const versionInfo = popplerVersionRegex.exec(stderr)[1];

const args = parseOptions(acceptedOptions, options, versionInfo);
files.forEach((element) => {
Expand Down

0 comments on commit 7c1296e

Please sign in to comment.