Skip to content

Commit 28dc848

Browse files
committed
lib: improve method of function calling
Using a more "direct" method of function calling yields better performance. PR-URL: #10789 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 403b89e commit 28dc848

File tree

3 files changed

+10
-11
lines changed

3 files changed

+10
-11
lines changed

lib/internal/module.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,22 @@ exports = module.exports = {
88

99
exports.requireDepth = 0;
1010

11-
// Invoke with makeRequireFunction.call(module) where |module| is the
12-
// Module object to use as the context for the require() function.
13-
function makeRequireFunction() {
14-
const Module = this.constructor;
15-
const self = this;
11+
// Invoke with makeRequireFunction(module) where |module| is the Module object
12+
// to use as the context for the require() function.
13+
function makeRequireFunction(mod) {
14+
const Module = mod.constructor;
1615

1716
function require(path) {
1817
try {
1918
exports.requireDepth += 1;
20-
return self.require(path);
19+
return mod.require(path);
2120
} finally {
2221
exports.requireDepth -= 1;
2322
}
2423
}
2524

2625
function resolve(request) {
27-
return Module._resolveFilename(request, self);
26+
return Module._resolveFilename(request, mod);
2827
}
2928

3029
require.resolve = resolve;

lib/module.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -577,11 +577,11 @@ Module.prototype._compile = function(content, filename) {
577577
}
578578
}
579579
var dirname = path.dirname(filename);
580-
var require = internalModule.makeRequireFunction.call(this);
581-
var args = [this.exports, require, this, filename, dirname];
580+
var require = internalModule.makeRequireFunction(this);
582581
var depth = internalModule.requireDepth;
583582
if (depth === 0) stat.cache = new Map();
584-
var result = compiledWrapper.apply(this.exports, args);
583+
var result = compiledWrapper.call(this.exports, this.exports, require, this,
584+
filename, dirname);
585585
if (depth === 0) stat.cache = null;
586586
return result;
587587
};

lib/repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ REPLServer.prototype.createContext = function() {
714714
const module = new Module('<repl>');
715715
module.paths = Module._resolveLookupPaths('<repl>', parentModule)[1];
716716

717-
const require = internalModule.makeRequireFunction.call(module);
717+
const require = internalModule.makeRequireFunction(module);
718718
context.module = module;
719719
context.require = require;
720720

0 commit comments

Comments
 (0)