Skip to content

Commit

Permalink
Returned false instead of array of 0's in case of miss
Browse files Browse the repository at this point in the history
This speads up single letter abbreviation misses by up to 10x, and 4 letter
misses by ~2x.

Updated a few `if (x == 0)` to use `===`, just to be safe.
  • Loading branch information
ratbeard authored and rmm5t committed Dec 18, 2011
1 parent 93107b0 commit a15c88a
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions liquidmetal.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ var LiquidMetal = function() {
return {
score: function(string, abbreviation) {
// Short circuits
if (abbreviation.length == 0) return SCORE_TRAILING;
if (abbreviation.length === 0) return SCORE_TRAILING;
if (abbreviation.length > string.length) return SCORE_NO_MATCH;

var scores = this.buildScoreArray(string, abbreviation);

// complete miss:
if ( scores === false ) return 0;

var sum = 0.0;
for (var i = 0; i < scores.length; i++) {
sum += scores[i];
Expand All @@ -45,8 +48,9 @@ var LiquidMetal = function() {
for (var i = 0; i < chars.length; i++) {
var c = chars[i];
var index = lower.indexOf(c, lastIndex+1);
if (index < 0) return fillArray(scores, SCORE_NO_MATCH);
if (index == 0) started = true;

if (index === -1) return false; // signal no match
if (index === 0) started = true;

if (isNewWord(string, index)) {
scores[index-1] = 1;
Expand Down

0 comments on commit a15c88a

Please sign in to comment.