Skip to content

Commit 798af4c

Browse files
author
Brian Vaughn
committed
Optimized TfIdfSearchIndex's indexDocument() method
1 parent a035d10 commit 798af4c

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

source/SearchIndex/TfIdfSearchIndex.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,33 @@ export class TfIdfSearchIndex implements ISearchIndex {
3838
/**
3939
* @inheritDocs
4040
*/
41-
indexDocument(token : string, uid : string, document : Object) : void {
41+
indexDocument(token : string, uid : string, doc : Object) : void {
4242
this._tokenToIdfCache = {}; // New index invalidates previous IDF caches
4343

44-
if (typeof this._tokenMap[token] !== 'object') {
45-
this._tokenMap[token] = {
44+
var tokenMap = this._tokenMap;
45+
var tokenDatum;
46+
47+
if (!tokenMap.hasOwnProperty(token)) {
48+
tokenMap[token] = tokenDatum = {
4649
$numDocumentOccurrences: 0,
4750
$totalNumOccurrences: 1,
4851
$uidMap: {},
4952
};
5053
} else {
51-
this._tokenMap[token].$totalNumOccurrences++;
54+
tokenDatum = tokenMap[token];
55+
tokenDatum.$totalNumOccurrences++;
5256
}
5357

54-
if (!this._tokenMap[token].$uidMap[uid]) {
55-
this._tokenMap[token].$numDocumentOccurrences++;
58+
var uidMap = tokenDatum.$uidMap;
5659

57-
this._tokenMap[token].$uidMap[uid] = {
58-
$document: document,
60+
if (!uidMap.hasOwnProperty(uid)) {
61+
tokenDatum.$numDocumentOccurrences++;
62+
uidMap[uid] = {
63+
$document: doc,
5964
$numTokenOccurrences: 1
6065
};
6166
} else {
62-
this._tokenMap[token].$uidMap[uid].$numTokenOccurrences++;
67+
uidMap[uid].$numTokenOccurrences++;
6368
}
6469
}
6570

0 commit comments

Comments
 (0)