Browse Source

module: avoid hasOwnProperty()

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 <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>
v6
Brian White 8 years ago
parent
commit
403b89e72b
No known key found for this signature in database GPG Key ID: 606D7358F94DA209
  1. 16
      lib/module.js

16
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.<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));

Loading…
Cancel
Save