Skip to content

Commit cfbd194

Browse files
author
Brian Vaughn
committed
Replaced object.hasOwnProperty(key) checks with typeof object[key] === 'undefined' for perf boost
1 parent eef97c0 commit cfbd194

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

source/SearchIndex/TfIdfSearchIndex.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export class TfIdfSearchIndex implements ISearchIndex {
4444
var tokenMap = this._tokenMap;
4545
var tokenDatum;
4646

47-
if (!tokenMap.hasOwnProperty(token)) {
47+
if (typeof tokenMap[token] !== 'object') {
4848
tokenMap[token] = tokenDatum = {
4949
$numDocumentOccurrences: 0,
5050
$totalNumOccurrences: 1,
@@ -57,7 +57,7 @@ export class TfIdfSearchIndex implements ISearchIndex {
5757

5858
var uidMap = tokenDatum.$uidMap;
5959

60-
if (!uidMap.hasOwnProperty(uid)) {
60+
if (typeof uidMap[uid] !== 'object') {
6161
tokenDatum.$numDocumentOccurrences++;
6262
uidMap[uid] = {
6363
$document: doc,
@@ -95,7 +95,7 @@ export class TfIdfSearchIndex implements ISearchIndex {
9595
for (var j = 0, numKeys = keys.length; j < numKeys; j++) {
9696
var uid = keys[j];
9797

98-
if (!tokenMetadata.$uidMap.hasOwnProperty(uid)) {
98+
if (typeof tokenMetadata.$uidMap[uid] !== 'object') {
9999
delete uidToDocumentMap[uid];
100100
}
101101
}
@@ -127,7 +127,7 @@ export class TfIdfSearchIndex implements ISearchIndex {
127127

128128
return function calculateIdf(token : string, documents : Array<Object>) : number {
129129
if (!tokenToIdfCache[token]) {
130-
var numDocumentsWithToken:number = tokenMap[token]
130+
var numDocumentsWithToken:number = typeof tokenMap[token] !== 'undefined'
131131
? tokenMap[token].$numDocumentOccurrences
132132
: 0;
133133

@@ -156,9 +156,11 @@ export class TfIdfSearchIndex implements ISearchIndex {
156156
}
157157

158158
var uid:any = document && document[uidFieldName];
159-
var termFrequency:number = tokenMap[token] && tokenMap[token].$uidMap[uid]
160-
? tokenMap[token].$uidMap[uid].$numTokenOccurrences
161-
: 0;
159+
var termFrequency:number =
160+
typeof tokenMap[token] !== 'undefined' &&
161+
typeof tokenMap[token].$uidMap[uid] !== 'undefined'
162+
? tokenMap[token].$uidMap[uid].$numTokenOccurrences
163+
: 0;
162164

163165
score += termFrequency * inverseDocumentFrequency;
164166
}

source/SearchIndex/UnorderedSearchIndex.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class UnorderedSearchIndex implements ISearchIndex {
1616
* @inheritDocs
1717
*/
1818
indexDocument(token : string, uid : string, doc : Object) : void {
19-
if (!this._tokenToUidToDocumentMap.hasOwnProperty(token)) {
19+
if (typeof this._tokenToUidToDocumentMap[token] !== 'object') {
2020
this._tokenToUidToDocumentMap[token] = {};
2121
}
2222

@@ -54,7 +54,7 @@ export class UnorderedSearchIndex implements ISearchIndex {
5454
for (var j = 0, numKeys = keys.length; j < numKeys; j++) {
5555
var uid = keys[j];
5656

57-
if (!documentMap.hasOwnProperty(uid)) {
57+
if (typeof documentMap[uid] !== 'object') {
5858
delete intersectingDocumentMap[uid];
5959
}
6060
}

0 commit comments

Comments
 (0)