Skip to content

Commit 998c2bb

Browse files
committed
feat(perf): Hoist RegExp literals in RequestShortener
1 parent d21beb2 commit 998c2bb

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

lib/RequestShortener.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,43 @@
55
"use strict";
66

77
const path = require("path");
8+
const NORMALIZE_SLASH_DIRECTION_REGEXP = /\\/g;
9+
const PATH_CHARS_REGEXP = /[-[\]{}()*+?.,\\^$|#\s]/g;
10+
const SEPERATOR_REGEXP = /[\/\\]$/;
11+
12+
13+
const normalizeBackSlashDirection = (request) => {
14+
return request.replace(NORMALIZE_SLASH_DIRECTION_REGEXP, "/");
15+
};
16+
17+
const shortenPath = (path) => {
18+
return path.replace(PATH_CHARS_REGEXP, "\\$&");
19+
};
20+
821

922
class RequestShortener {
1023
constructor(directory) {
11-
directory = directory.replace(/\\/g, "/");
24+
directory = normalizeBackSlashDirection(directory);
1225
if(/[\/\\]$/.test(directory)) directory = directory.substr(0, directory.length - 1);
1326

1427
if(directory) {
15-
const currentDirectoryRegExpString = directory.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
28+
const currentDirectoryRegExpString = shortenPath(directory);
1629
this.currentDirectoryRegExp = new RegExp("^" + currentDirectoryRegExpString + "|(!)" + currentDirectoryRegExpString, "g");
1730
}
1831

1932
const dirname = path.dirname(directory);
20-
const endsWithSeperator = /[\/\\]$/.test(dirname);
33+
const endsWithSeperator = SEPERATOR_REGEXP.test(dirname);
2134
const parentDirectory = endsWithSeperator ? dirname.substr(0, dirname.length - 1) : dirname;
2235
if(parentDirectory && parentDirectory !== directory) {
23-
const parentDirectoryRegExpString = parentDirectory.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
36+
const parentDirectoryRegExpString = shortenPath(parentDirectory);
2437
this.parentDirectoryRegExp = new RegExp("^" + parentDirectoryRegExpString + "|(!)" + parentDirectoryRegExpString, "g");
2538
}
2639

2740
if(__dirname.length >= 2) {
28-
const buildins = path.join(__dirname, "..").replace(/\\/g, "/");
41+
const buildins = normalizeBackSlashDirection(path.join(__dirname, ".."));
2942
const buildinsAsModule = this.currentDirectoryRegExp && this.currentDirectoryRegExp.test(buildins);
3043
this.buildinsAsModule = buildinsAsModule;
31-
const buildinsRegExpString = buildins.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
44+
const buildinsRegExpString = shortenPath(buildins);
3245
this.buildinsRegExp = new RegExp("^" + buildinsRegExpString + "|(!)" + buildinsRegExpString, "g");
3346
}
3447

@@ -37,7 +50,7 @@ class RequestShortener {
3750

3851
shorten(request) {
3952
if(!request) return request;
40-
request = request.replace(/\\/g, "/");
53+
request = normalizeBackSlashDirection(request);
4154
if(this.buildinsAsModule && this.buildinsRegExp)
4255
request = request.replace(this.buildinsRegExp, "!(webpack)");
4356
if(this.currentDirectoryRegExp)

0 commit comments

Comments
 (0)