From 403b89e72b6367934ca3c36d389ce0f3214ffbf5 Mon Sep 17 00:00:00 2001 From: Brian White Date: Fri, 13 Jan 2017 05:13:53 -0500 Subject: [PATCH] module: avoid hasOwnProperty() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hasOwnProperty() is known to be slow, do a direct lookup on a "clean" object instead. 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/module.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/lib/module.js b/lib/module.js index bd6af190f642b1..8916b3b36c2e6f 100644 --- a/lib/module.js +++ b/lib/module.js @@ -33,14 +33,6 @@ const internalModuleReadFile = process.binding('fs').internalModuleReadFile; const internalModuleStat = process.binding('fs').internalModuleStat; const preserveSymlinks = !!process.binding('config').preserveSymlinks; -// If obj.hasOwnProperty has been overridden, then calling -// obj.hasOwnProperty(prop) will break. -// See: https://github.com/joyent/node/issues/1707 -function hasOwnProperty(obj, prop) { - return Object.prototype.hasOwnProperty.call(obj, prop); -} - - function stat(filename) { filename = path._makeLong(filename); const cache = stat.cache; @@ -95,12 +87,12 @@ const debug = Module._debug; // -> a/index. // check if the directory is a package.json dir -const packageMainCache = {}; +const packageMainCache = Object.create(null); function readPackage(requestPath) { - if (hasOwnProperty(packageMainCache, requestPath)) { - return packageMainCache[requestPath]; - } + const entry = packageMainCache[requestPath]; + if (entry) + return entry; const jsonPath = path.resolve(requestPath, 'package.json'); const json = internalModuleReadFile(path._makeLong(jsonPath));