From 28dc848e702b70f8c07941a1dfd46227f90c8267 Mon Sep 17 00:00:00 2001 From: Brian White Date: Fri, 13 Jan 2017 05:16:46 -0500 Subject: [PATCH] lib: improve method of function calling MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Using a more "direct" method of function calling yields better performance. PR-URL: https://github.com/nodejs/node/pull/10789 Reviewed-By: James M Snell Reviewed-By: Michaƫl Zasso Reviewed-By: Michael Dawson Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum --- lib/internal/module.js | 13 ++++++------- lib/module.js | 6 +++--- lib/repl.js | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/internal/module.js b/lib/internal/module.js index 2f38618daac5f7..8fc8dfbf327e61 100644 --- a/lib/internal/module.js +++ b/lib/internal/module.js @@ -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; diff --git a/lib/module.js b/lib/module.js index 8916b3b36c2e6f..1b9c2413b7ce9e 100644 --- a/lib/module.js +++ b/lib/module.js @@ -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; }; diff --git a/lib/repl.js b/lib/repl.js index 3dd243b0b0cdbf..676fa105861d33 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -714,7 +714,7 @@ REPLServer.prototype.createContext = function() { const module = new Module(''); module.paths = Module._resolveLookupPaths('', parentModule)[1]; - const require = internalModule.makeRequireFunction.call(module); + const require = internalModule.makeRequireFunction(module); context.module = module; context.require = require;