mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
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>
This commit is contained in:
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user