Skip to content

Commit 7f1e308

Browse files
minor 20% optimization to lookup
1 parent 9e3aec6 commit 7f1e308

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

src/namespace.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,26 +404,36 @@ Namespace.prototype.lookup = function lookup(path, filterTypes, parentAlreadyChe
404404
// Start at root if path is absolute
405405
if (path[0] === "")
406406
return this.root.lookup(path.slice(1), filterTypes);
407+
var flatPath = path.join(".");
407408

408-
var found = this._lookupImpl(path);
409+
var found = this._lookupImpl(path, flatPath);
409410
if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
410411
return found;
411412
}
412413

413-
// If there hasn't been a match, try again at the parent
414-
if (this.parent === null || parentAlreadyChecked)
414+
// If there hasn't been a match, walk up the tree
415+
if (parentAlreadyChecked)
415416
return null;
416-
return this.parent.lookup(path, filterTypes);
417+
418+
var current = this;
419+
while (current.parent) {
420+
found = current.parent._lookupImpl(path, flatPath);
421+
if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
422+
return found;
423+
}
424+
current = current.parent;
425+
}
426+
return null;
417427
};
418428

419429
/**
420430
* Internal helper for lookup that handles searching just at this namespace and below along with caching.
421431
* @param {string[]} path Path to look up
432+
* @param {string} flatPath Flattened version of the path to use as a cache key
422433
* @returns {ReflectionObject|null} Looked up object or `null` if none could be found
423434
* @private
424435
*/
425-
Namespace.prototype._lookupImpl = function lookup(path) {
426-
var flatPath = path.join(".");
436+
Namespace.prototype._lookupImpl = function lookup(path, flatPath) {
427437
if(Object.prototype.hasOwnProperty.call(this._lookupCache, flatPath)) {
428438
return this._lookupCache[flatPath];
429439
}
@@ -434,13 +444,15 @@ Namespace.prototype._lookupImpl = function lookup(path) {
434444
if (found) {
435445
if (path.length === 1) {
436446
exact = found;
437-
} else if (found instanceof Namespace && (found = found._lookupImpl(path.slice(1))))
438-
exact = found;
447+
} else if (found instanceof Namespace) {
448+
path = path.slice(1);
449+
exact = found._lookupImpl(path, path.join("."));
450+
}
439451

440452
// Otherwise try each nested namespace
441453
} else {
442454
for (var i = 0; i < this.nestedArray.length; ++i)
443-
if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i]._lookupImpl(path)))
455+
if (this._nestedArray[i] instanceof Namespace && (found = this._nestedArray[i]._lookupImpl(path, flatPath)))
444456
exact = found;
445457
}
446458

0 commit comments

Comments
 (0)