Permalink
Browse files

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 <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 Jan 13, 2017
1 parent 403b89e commit 28dc848e702b70f8c07941a1dfd46227f90c8267
Showing with 10 additions and 11 deletions.
  1. +6 −7 lib/internal/module.js
  2. +3 −3 lib/module.js
  3. +1 −1 lib/repl.js
View
@@ -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;
View
@@ -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;
};
View
@@ -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;

0 comments on commit 28dc848

Please sign in to comment.