Skip to content

Commit

Permalink
Don't store empty arrays for words with no flags.
Browse files Browse the repository at this point in the history
This change achieves a 35% decrease in memory usage for the demo I use to check things like that.

See #39.
  • Loading branch information
cfinke committed Apr 20, 2016
1 parent 2e55abd commit aa5b1f9
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions typo/typo.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,11 +431,17 @@ Typo.prototype = {

function addWord(word, rules) {
// Some dictionaries will list the same word multiple times with different rule sets.
if (!(word in dictionaryTable) || typeof dictionaryTable[word] != 'object') {
dictionaryTable[word] = [];
if (!dictionaryTable.hasOwnProperty(word)) {
dictionaryTable[word] = null;
}

dictionaryTable[word].push(rules);
if (rules.length > 0) {
if (dictionaryTable[word] === null) {
dictionaryTable[word] = [];
}

dictionaryTable[word].push(rules);
}
}

// The first line is the number of words in the dictionary.
Expand Down Expand Up @@ -675,18 +681,21 @@ Typo.prototype = {
}
}
}

return false;
}
else if (ruleCodes === null) {
// a null (but not undefined) value for an entry in the dictionary table
// means that the word is in the dictionary but has no flags.
return true;
}
else if (typeof ruleCodes === 'object') { // this.dictionary['hasOwnProperty'] will be a function.
for (i = 0, _len = ruleCodes.length; i < _len; i++) {
if (!this.hasFlag(word, "ONLYINCOMPOUND", ruleCodes[i])) {
return true;
}
}

return false;
}

return false;
},

/**
Expand Down

0 comments on commit aa5b1f9

Please sign in to comment.