diff --git a/lib/search.js b/lib/search.js index ef4b65a..ba399a7 100644 --- a/lib/search.js +++ b/lib/search.js @@ -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]; }); }); };