Skip to content

Commit 403b89e

Browse files
committed
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 <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 298a40e commit 403b89e

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

lib/module.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ const internalModuleReadFile = process.binding('fs').internalModuleReadFile;
3333
const internalModuleStat = process.binding('fs').internalModuleStat;
3434
const preserveSymlinks = !!process.binding('config').preserveSymlinks;
3535

36-
// If obj.hasOwnProperty has been overridden, then calling
37-
// obj.hasOwnProperty(prop) will break.
38-
// See: https://github.com/joyent/node/issues/1707
39-
function hasOwnProperty(obj, prop) {
40-
return Object.prototype.hasOwnProperty.call(obj, prop);
41-
}
42-
43-
4436
function stat(filename) {
4537
filename = path._makeLong(filename);
4638
const cache = stat.cache;
@@ -95,12 +87,12 @@ const debug = Module._debug;
9587
// -> a/index.<ext>
9688

9789
// check if the directory is a package.json dir
98-
const packageMainCache = {};
90+
const packageMainCache = Object.create(null);
9991

10092
function readPackage(requestPath) {
101-
if (hasOwnProperty(packageMainCache, requestPath)) {
102-
return packageMainCache[requestPath];
103-
}
93+
const entry = packageMainCache[requestPath];
94+
if (entry)
95+
return entry;
10496

10597
const jsonPath = path.resolve(requestPath, 'package.json');
10698
const json = internalModuleReadFile(path._makeLong(jsonPath));

0 commit comments

Comments
 (0)