Skip to content

Commit

Permalink
module: avoid hasOwnProperty()
Browse files Browse the repository at this point in the history
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 Mar 11, 2017
1 parent 298a40e commit 403b89e
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions lib/module.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 403b89e

Please sign in to comment.