diff options
| author | Niklas Baumstark | 2012-01-23 23:11:45 +0100 |
|---|---|---|
| committer | Niklas Baumstark | 2012-04-10 23:54:38 +0200 |
| commit | 14cb3e4f8e552c082cae799f8dca511c6c267891 (patch) | |
| tree | bc241c4cf680898ca4751f7c0ff5636c5d0093e7 /lib/completion.js | |
| parent | 6bc1d44939c9b4557ef8648a6e645e73caaa0623 (diff) | |
| download | vimium-14cb3e4f8e552c082cae799f8dca511c6c267891.tar.bz2 | |
minor optimizations and code cleanup
Diffstat (limited to 'lib/completion.js')
| -rw-r--r-- | lib/completion.js | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/lib/completion.js b/lib/completion.js index 1c4d60c0..04899ca8 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -6,6 +6,7 @@ var completion = (function() { var fuzzyMatcher = (function() { var self = {}; + self.timeToClean = 0; self.matcherCacheSize = 300; self.regexNonWord = /[\W_]/ig; @@ -121,6 +122,7 @@ var completion = (function() { // find the most specific list of sources in the cache var maxSpecificity = 0; + var specificity; for (key in self.filterCache[id]) { if (!self.filterCache[id].hasOwnProperty(key)) continue; @@ -132,24 +134,23 @@ var completion = (function() { } // is this cache entry the most specific so far? - var specificity = self.filterCache[id][key].length; + specificity = self.filterCache[id][key].length; if (query.indexOf(key) == 0 && specificity > maxSpecificity) { source = self.filterCache[id][key]; maxSpecificity = specificity; } } - // clean up - self.cleanMatcherCache(); + // clean up every few calls + if (++self.timeToClean > 20) { + self.timeToClean = 0; + self.cleanMatcherCache(); + } var matcher = self.getMatcher(query); - for (var i = 0; i < source.length; ++i) { - if (!matcher.test(getValue(source[i]))) - continue; - filtered.push(source[i]); - callback(source[i]); - } + var filtered = source.filter(function(x) { return matcher.test(getValue(x)) }); self.filterCache[id][query] = filtered; + return filtered; } return self; @@ -274,17 +275,13 @@ var completion = (function() { var self = this; var handler = function(results) { - var filtered = []; - fuzzyMatcher.filter(query, - results, self.extractStringFromMatch, - self.id, - function(match) { - filtered.push(createHighlightingCompletion( + var filtered = fuzzyMatcher.filter(query, results, self.extractStringFromMatch, self.id); + callback(filtered.map(function(match) { + return createHighlightingCompletion( query, match.str, self.createAction(match), - self.calculateRelevancy(query, match))); - }); - callback(filtered); + self.calculateRelevancy(query, match)); + })); } // are the results ready? |
