Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add a cache to reduce time-complexity of fuzzySuggest

in the case where there are many repeated characters.
  • Loading branch information...
commit e3123b42a32cb346678cf1d82eddf6286ced697f 1 parent 0fbf80c
Conrad Irwin ConradIrwin authored

Showing 1 changed file with 36 additions and 23 deletions. Show diff stats Hide diff stats

  1. +36 23 jquery.fuzzymatch.js
59 jquery.fuzzymatch.js
@@ -104,6 +104,9 @@
104 104 * @param abbreviation, an abbreviation that a user may have typed
105 105 * in order to specify that string.
106 106 *
  107 + * @cache (private), a cache that reduces the expected running time of the
  108 + * algorithm in the case there are many repeated characters.
  109 + *
107 110 * @return {
108 111 * score: A score (0 <= score <= 1) that indicates how likely it is that
109 112 * the abbreviation matches the string.
@@ -122,7 +125,7 @@
122 125 *
123 126 * }
124 127 **/
125   - $.fuzzyMatch = function (string, abbreviation) {
  128 + $.fuzzyMatch = function (string, abbreviation, cache) {
126 129 if (abbreviation === "") {
127 130 return {
128 131 score: string === "" ? SCORE_CONTINUE_MATCH : PENALTY_NOT_COMPLETE,
@@ -130,40 +133,50 @@
130 133 };
131 134 }
132 135
133   - return $(allCaseInsensitiveSplits(string, abbreviation.charAt(0)))
134   - .map(function (i, split) {
135   - var result = $.fuzzyMatch(split.after, abbreviation.slice(1)),
136   - preceding_char = split.before.charAt(split.before.length - 1);
  136 + if (cache && cache[string] && cache[string][abbreviation]) {
  137 + return $.extend({}, cache[string][abbreviation]);
  138 + }
  139 +
  140 + cache = cache || {};
  141 + cache[string] = cache[string] || {};
  142 + cache[string][abbreviation] =
137 143
138   - if (split.before === "") {
139   - result.score *= SCORE_CONTINUE_MATCH;
  144 + $(allCaseInsensitiveSplits(string, abbreviation.charAt(0)))
  145 + .map(function (i, split) {
  146 + var result = $.fuzzyMatch(split.after, abbreviation.slice(1), cache),
  147 + preceding_char = split.before.charAt(split.before.length - 1);
140 148
141   - } else if (preceding_char.match(/[\\\/\-_+.# \t"@\[\(\{&]/) ||
142   - (split.chr.toLowerCase() !== split.chr && preceding_char.toLowerCase() === preceding_char)) {
  149 + if (split.before === "") {
  150 + result.score *= SCORE_CONTINUE_MATCH;
143 151
144   - result.score *= SCORE_START_WORD;
145   - } else {
146   - result.score *= SCORE_OK;
147   - }
  152 + } else if (preceding_char.match(/[\\\/\-_+.# \t"@\[\(\{&]/) ||
  153 + (split.chr.toLowerCase() !== split.chr && preceding_char.toLowerCase() === preceding_char)) {
148 154
149   - if (split.chr !== abbreviation.charAt(0)) {
150   - result.score *= PENALTY_CASE_MISMATCH;
151   - }
  155 + result.score *= SCORE_START_WORD;
  156 + } else {
  157 + result.score *= SCORE_OK;
  158 + }
152 159
153   - result.score *= Math.pow(PENALTY_SKIPPED, split.before.length);
154   - result.html = $('<div>').text(split.before).append($('<b>').text(split.chr)).append(result.html).html();
  160 + if (split.chr !== abbreviation.charAt(0)) {
  161 + result.score *= PENALTY_CASE_MISMATCH;
  162 + }
155 163
156   - return result;
157   - })
158   - .sort(function (a, b) {
159   - return a.score < b.score ? 1 : a.score === b.score ? 0 : -1;
160   - })[0] ||
  164 + result.score *= Math.pow(PENALTY_SKIPPED, split.before.length);
  165 + result.html = $('<div>').text(split.before).append($('<b>').text(split.chr)).append(result.html).html();
  166 +
  167 + return result;
  168 + })
  169 + .sort(function (a, b) {
  170 + return a.score < b.score ? 1 : a.score === b.score ? 0 : -1;
  171 + })[0] ||
161 172
162 173 // No matches for the next character in the abbreviation, abort!
163 174 {
164 175 score: 0, // This 0 will multiply up to the top, giving a total of 0
165 176 html: $('<div>').text(string).html()
166 177 };
  178 +
  179 + return $.extend({}, cache[string][abbreviation]);
167 180 };
168 181 /*global jQuery */
169 182 }(jQuery));

0 comments on commit e3123b4

Please sign in to comment.
Something went wrong with that request. Please try again.