Skip to content

Commit 8a264ae

Browse files
evanlucasrvagg
authored andcommitted
module: cache regular expressions
None of these regular expressions will change, so there is no need to generate them every time in hot code paths. Provides a small performance improvement in module loading. (5-10%) PR-URL: #3869 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 654192f commit 8a264ae

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

lib/module.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ const path = require('path');
1111
const internalModuleReadFile = process.binding('fs').internalModuleReadFile;
1212
const internalModuleStat = process.binding('fs').internalModuleStat;
1313

14+
const splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//;
15+
const isIndexRe = /^index\.\w+?$/;
16+
const shebangRe = /^\#\!.*/;
1417

1518
// If obj.hasOwnProperty has been overridden, then calling
1619
// obj.hasOwnProperty(prop) will break.
@@ -191,7 +194,6 @@ Module._nodeModulePaths = function(from) {
191194
// note: this approach *only* works when the path is guaranteed
192195
// to be absolute. Doing a fully-edge-case-correct path.split
193196
// that works on both Windows and Posix is non-trivial.
194-
var splitRe = process.platform === 'win32' ? /[\/\\]/ : /\//;
195197
var paths = [];
196198
var parts = from.split(splitRe);
197199

@@ -244,7 +246,7 @@ Module._resolveLookupPaths = function(request, parent) {
244246
// Is the parent an index module?
245247
// We can assume the parent has a valid extension,
246248
// as it already has been accepted as a module.
247-
var isIndex = /^index\.\w+?$/.test(path.basename(parent.filename));
249+
var isIndex = isIndexRe.test(path.basename(parent.filename));
248250
var parentIdPath = isIndex ? parent.id : path.dirname(parent.id);
249251
var id = path.resolve(parentIdPath, request);
250252

@@ -377,7 +379,7 @@ var resolvedArgv;
377379
Module.prototype._compile = function(content, filename) {
378380
var self = this;
379381
// remove shebang
380-
content = content.replace(/^\#\!.*/, '');
382+
content = content.replace(shebangRe, '');
381383

382384
function require(path) {
383385
return self.require(path);

0 commit comments

Comments
 (0)