Skip to content

Commit

Permalink
perf: improve creating tfIdf with cache (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
vivekjoshi556 authored Feb 4, 2024
1 parent d38403c commit 0df16ea
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,15 @@ let getIdf = (word) => {

let createTfIdf = () => {
corpus.tfidf = {};
let idfCache = {};

Object.keys(corpus.fileWords).forEach((file) => {
corpus.tfidf[file] = {};
Object.keys(corpus.fileWords[file]).forEach((word) => {
let tfidf = getTf(word, file) * getIdf(word);
corpus.tfidf[file][word] = tfidf;
if(!(word in idfCache)) {
idfCache[word] = getIdf(word);
}
corpus.tfidf[file][word] = getTf(word, file) * idfCache[word];
});
});
};
Expand Down

0 comments on commit 0df16ea

Please sign in to comment.