Permalink
Browse files

module: avoid hasOwnProperty()

hasOwnProperty() is known to be slow, do a direct lookup on a "clean"
object instead.

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 298a40e commit 403b89e72b6367934ca3c36d389ce0f3214ffbf5
Showing with 4 additions and 12 deletions.
  1. +4 −12 lib/module.js
View
@@ -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.<ext>
// 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));

0 comments on commit 403b89e

Please sign in to comment.