Skip to content

Commit

Permalink
performance optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaong committed Sep 18, 2020
1 parent 20bca94 commit ace6f25
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/MiniSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@ const combinators: { [kind: string]: CombinatorFunction } = {
} else {
combined[documentId].score += score
combined[documentId].score *= 1.5
combined[documentId].terms = [...combined[documentId].terms, ...terms]
combined[documentId].terms.push(...terms)
Object.assign(combined[documentId].match, match)
}
return combined
Expand Down
13 changes: 10 additions & 3 deletions src/SearchableMap/SearchableMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,16 @@ class SearchableMap<T = any> implements IterableSet<T> {

const trackDown = <T = any>(tree: RadixTree<T> | undefined, key: string, path: Path<T> = []): [RadixTree<T> | undefined, Path<T>] => {
if (key.length === 0 || tree == null) { return [tree, path] }
const nodeKey = Object.keys(tree || {}).find(k => k !== LEAF && key.startsWith(k))
if (nodeKey === undefined) { return trackDown(undefined, '', [...path, [tree, key]]) }
return trackDown(tree[nodeKey] as RadixTree<T>, key.slice(nodeKey.length), [...path, [tree, nodeKey]])

const nodeKey = Object.keys(tree).find(k => k !== LEAF && key.startsWith(k))

if (nodeKey === undefined) {
path.push([tree, key]) // performance: update in place
return trackDown(undefined, '', path)
}

path.push([tree, nodeKey]) // performance: update in place
return trackDown(tree[nodeKey] as RadixTree<T>, key.slice(nodeKey.length), path)
}

const lookup = <T = any>(tree: RadixTree<T>, key: string): RadixTree<T> | undefined => {
Expand Down

0 comments on commit ace6f25

Please sign in to comment.