Skip to content

Commit a5e28aa

Browse files
mrjfljharb
authored andcommitted
[Refactor] hoist regex expressions for efficiency
1 parent 8f764b5 commit a5e28aa

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"func-style": 0,
2020
"global-require": 1,
2121
"id-length": [2, { "min": 1, "max": 40 }],
22-
"max-lines": [2, 350],
22+
"max-lines": [2, 360],
2323
"max-lines-per-function": 1,
2424
"max-nested-callbacks": 0,
2525
"max-params": 0,

lib/async.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ var isCore = require('is-core-module');
88

99
var realpathFS = process.platform !== 'win32' && fs.realpath && typeof fs.realpath.native === 'function' ? fs.realpath.native : fs.realpath;
1010

11+
var relativePathRegex = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/;
12+
var windowsDriveRegex = /^\w:[/\\]*$/;
13+
var nodeModulesRegex = /[/\\]node_modules[/\\]*$/;
14+
1115
var homedir = getHomedir();
1216
var defaultPaths = function () {
1317
return [
@@ -138,10 +142,10 @@ module.exports = function resolve(x, options, callback) {
138142

139143
var res;
140144
function validBasedir(basedir) {
141-
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
145+
if (relativePathRegex.test(x)) {
142146
res = path.resolve(basedir, x);
143147
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
144-
if ((/\/$/).test(x) && res === basedir) {
148+
if (x.slice(-1) === '/' && res === basedir) {
145149
loadAsDirectory(res, opts.package, onfile);
146150
} else loadAsFile(res, opts.package, onfile);
147151
} else if (includeCoreModules && isCore(x)) {
@@ -229,10 +233,10 @@ module.exports = function resolve(x, options, callback) {
229233

230234
function loadpkg(dir, cb) {
231235
if (dir === '' || dir === '/') return cb(null);
232-
if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) {
236+
if (process.platform === 'win32' && windowsDriveRegex.test(dir)) {
233237
return cb(null);
234238
}
235-
if ((/[/\\]node_modules[/\\]*$/).test(dir)) return cb(null);
239+
if (nodeModulesRegex.test(dir)) return cb(null);
236240

237241
maybeRealpath(realpath, dir, opts, function (unwrapErr, pkgdir) {
238242
if (unwrapErr) return loadpkg(path.dirname(dir), cb);

lib/node-modules-paths.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
var path = require('path');
22
var parse = path.parse || require('path-parse'); // eslint-disable-line global-require
33

4+
var driveLetterRegex = /^([A-Za-z]:)/;
5+
var uncPathRegex = /^\\\\/;
6+
47
var getNodeModulesDirs = function getNodeModulesDirs(absoluteStart, modules) {
58
var prefix = '/';
6-
if ((/^([A-Za-z]:)/).test(absoluteStart)) {
9+
if (driveLetterRegex.test(absoluteStart)) {
710
prefix = '';
8-
} else if ((/^\\\\/).test(absoluteStart)) {
11+
} else if (uncPathRegex.test(absoluteStart)) {
912
prefix = '\\\\';
1013
}
1114

lib/sync.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ var normalizeOptions = require('./normalize-options');
88

99
var realpathFS = process.platform !== 'win32' && fs.realpathSync && typeof fs.realpathSync.native === 'function' ? fs.realpathSync.native : fs.realpathSync;
1010

11+
var relativePathRegex = /^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/;
12+
var windowsDriveRegex = /^\w:[/\\]*$/;
13+
var nodeModulesRegex = /[/\\]node_modules[/\\]*$/;
14+
1115
var homedir = getHomedir();
1216
var defaultPaths = function () {
1317
return [
@@ -98,7 +102,7 @@ module.exports = function resolveSync(x, options) {
98102
throw dirError;
99103
}
100104

101-
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
105+
if (relativePathRegex.test(x)) {
102106
var res = path.resolve(absoluteStart, x);
103107
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
104108
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
@@ -139,10 +143,10 @@ module.exports = function resolveSync(x, options) {
139143

140144
function loadpkg(dir) {
141145
if (dir === '' || dir === '/') return;
142-
if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) {
146+
if (process.platform === 'win32' && windowsDriveRegex.test(dir)) {
143147
return;
144148
}
145-
if ((/[/\\]node_modules[/\\]*$/).test(dir)) return;
149+
if (nodeModulesRegex.test(dir)) return;
146150

147151
var pkgfile = path.join(isDirectory(dir) ? maybeRealpathSync(realpathSync, dir, opts) : dir, 'package.json');
148152

0 commit comments

Comments
 (0)