Skip to content

Commit

Permalink
lib: improve method of function calling
Browse files Browse the repository at this point in the history
Using a more "direct" method of function calling yields better
performance.

PR-URL: #10789
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
mscdex committed Mar 11, 2017
1 parent 403b89e commit 28dc848
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
13 changes: 6 additions & 7 deletions lib/internal/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@ exports = module.exports = {

exports.requireDepth = 0;

// Invoke with makeRequireFunction.call(module) where |module| is the
// Module object to use as the context for the require() function.
function makeRequireFunction() {
const Module = this.constructor;
const self = this;
// Invoke with makeRequireFunction(module) where |module| is the Module object
// to use as the context for the require() function.
function makeRequireFunction(mod) {
const Module = mod.constructor;

function require(path) {
try {
exports.requireDepth += 1;
return self.require(path);
return mod.require(path);
} finally {
exports.requireDepth -= 1;
}
}

function resolve(request) {
return Module._resolveFilename(request, self);
return Module._resolveFilename(request, mod);
}

require.resolve = resolve;
Expand Down
6 changes: 3 additions & 3 deletions lib/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,11 @@ Module.prototype._compile = function(content, filename) {
}
}
var dirname = path.dirname(filename);
var require = internalModule.makeRequireFunction.call(this);
var args = [this.exports, require, this, filename, dirname];
var require = internalModule.makeRequireFunction(this);
var depth = internalModule.requireDepth;
if (depth === 0) stat.cache = new Map();
var result = compiledWrapper.apply(this.exports, args);
var result = compiledWrapper.call(this.exports, this.exports, require, this,
filename, dirname);
if (depth === 0) stat.cache = null;
return result;
};
Expand Down
2 changes: 1 addition & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ REPLServer.prototype.createContext = function() {
const module = new Module('<repl>');
module.paths = Module._resolveLookupPaths('<repl>', parentModule)[1];

const require = internalModule.makeRequireFunction.call(module);
const require = internalModule.makeRequireFunction(module);
context.module = module;
context.require = require;

Expand Down

0 comments on commit 28dc848

Please sign in to comment.