Skip to content

Commit c0e3469

Browse files
mrjfljharb
authored andcommitted
[Refactor] hoist regex expressions for efficiency (#336)
1 parent d36d57e commit c0e3469

4 files changed

Lines changed: 21 additions & 10 deletions

File tree

.eslintrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"func-style": 0,
1515
"global-require": 1,
1616
"id-length": [2, { "min": 1, "max": 40 }],
17-
"max-lines": [2, 350],
17+
"max-lines": [2, 360],
1818
"max-lines-per-function": 0,
1919
"max-nested-callbacks": 0,
2020
"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 [
@@ -124,10 +128,10 @@ module.exports = function resolve(x, options, callback) {
124128

125129
var res;
126130
function init(basedir) {
127-
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
131+
if (relativePathRegex.test(x)) {
128132
res = path.resolve(basedir, x);
129133
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
130-
if ((/\/$/).test(x) && res === basedir) {
134+
if (x.slice(-1) === '/' && res === basedir) {
131135
loadAsDirectory(res, opts.package, onfile);
132136
} else loadAsFile(res, opts.package, onfile);
133137
} else if (includeCoreModules && isCore(x)) {
@@ -215,10 +219,10 @@ module.exports = function resolve(x, options, callback) {
215219

216220
function loadpkg(dir, cb) {
217221
if (dir === '' || dir === '/') return cb(null);
218-
if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) {
222+
if (process.platform === 'win32' && windowsDriveRegex.test(dir)) {
219223
return cb(null);
220224
}
221-
if ((/[/\\]node_modules[/\\]*$/).test(dir)) return cb(null);
225+
if (nodeModulesRegex.test(dir)) return cb(null);
222226

223227
maybeRealpath(realpath, dir, opts, function (unwrapErr, pkgdir) {
224228
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 [
@@ -96,7 +100,7 @@ module.exports = function resolveSync(x, options) {
96100
// ensure that `basedir` is an absolute path at this point, resolving against the process' current working directory
97101
var absoluteStart = maybeRealpathSync(realpathSync, path.resolve(basedir), opts);
98102

99-
if ((/^(?:\.\.?(?:\/|$)|\/|([A-Za-z]:)?[/\\])/).test(x)) {
103+
if (relativePathRegex.test(x)) {
100104
var res = path.resolve(absoluteStart, x);
101105
if (x === '.' || x === '..' || x.slice(-1) === '/') res += '/';
102106
var m = loadAsFileSync(res) || loadAsDirectorySync(res);
@@ -137,10 +141,10 @@ module.exports = function resolveSync(x, options) {
137141

138142
function loadpkg(dir) {
139143
if (dir === '' || dir === '/') return;
140-
if (process.platform === 'win32' && (/^\w:[/\\]*$/).test(dir)) {
144+
if (process.platform === 'win32' && windowsDriveRegex.test(dir)) {
141145
return;
142146
}
143-
if ((/[/\\]node_modules[/\\]*$/).test(dir)) return;
147+
if (nodeModulesRegex.test(dir)) return;
144148

145149
var pkgfile = path.join(maybeRealpathSync(realpathSync, dir, opts), 'package.json');
146150

0 commit comments

Comments
 (0)