Skip to content

Commit 0df16ea

Browse files
perf: improve creating tfIdf with cache (#433)
1 parent d38403c commit 0df16ea

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

lib/search.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,15 @@ let getIdf = (word) => {
8888

8989
let createTfIdf = () => {
9090
corpus.tfidf = {};
91+
let idfCache = {};
92+
9193
Object.keys(corpus.fileWords).forEach((file) => {
9294
corpus.tfidf[file] = {};
9395
Object.keys(corpus.fileWords[file]).forEach((word) => {
94-
let tfidf = getTf(word, file) * getIdf(word);
95-
corpus.tfidf[file][word] = tfidf;
96+
if(!(word in idfCache)) {
97+
idfCache[word] = getIdf(word);
98+
}
99+
corpus.tfidf[file][word] = getTf(word, file) * idfCache[word];
96100
});
97101
});
98102
};

0 commit comments

Comments
 (0)