Skip to content

Commit

Permalink
Remove unnecessary length checks + improved indexing of charCodeCache (
Browse files Browse the repository at this point in the history
  • Loading branch information
gustf authored and sindresorhus committed Jun 14, 2017
1 parent 0630566 commit 37573fe
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ module.exports = function (a, b) {
var aLen = a.length;
var bLen = b.length;

if (aLen === 0) {
return bLen;
}

if (bLen === 0) {
return aLen;
}

// Performing suffix trimming:
// We can linearly drop suffix common to both strings since they
// don't increase distance at all
Expand All @@ -37,10 +29,6 @@ module.exports = function (a, b) {
bLen--;
}

if (aLen === 0) {
return bLen;
}

// Performing prefix trimming
// We can linearly drop prefix common to both strings since they
// don't increase distance at all
Expand All @@ -65,7 +53,7 @@ module.exports = function (a, b) {
var j = 0;

while (i < aLen) {
charCodeCache[start + i] = a.charCodeAt(start + i);
charCodeCache[i] = a.charCodeAt(start + i);
arr[i] = ++i;
}

Expand All @@ -75,7 +63,7 @@ module.exports = function (a, b) {
ret = j;

for (i = 0; i < aLen; i++) {
tmp2 = bCharCode === charCodeCache[start + i] ? tmp : tmp + 1;
tmp2 = bCharCode === charCodeCache[i] ? tmp : tmp + 1;
tmp = arr[i];
ret = arr[i] = tmp > ret ? tmp2 > ret ? ret + 1 : tmp2 : tmp2 > tmp ? tmp + 1 : tmp2;
}
Expand Down

0 comments on commit 37573fe

Please sign in to comment.