Skip to content

Commit 4fb27ee

Browse files
author
rafaelk
committed
Performance tweaks for soundex
1 parent a77c29b commit 4fb27ee

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

functions/strings/soundex.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,22 @@ function soundex (str) {
1515
// * example 3: soundex('Euler');
1616
// * returns 3: 'E460'
1717

18-
var upStr = (str+'').toUpperCase();
19-
var sdx = [upStr.charAt(0),0,0,0],
20-
m = {BFPV: 1, CGJKQSXZ: 2, DT: 3, L: 4, MN: 5, R: 6 },
21-
k = ['BFPV', 'CGJKQSXZ', 'DT', 'L', 'MN', 'R'],
22-
i = 1, j = 0, s = 0, key, code,
23-
l = upStr.length;
18+
str = (str + '').toUpperCase();
19+
var sdx = [str.charAt(0), 0, 0 , 0],
20+
k = ['BFPV', 'CGJKQSXZ', 'DT', 'L', 'MN', 'R'], kl = k.length,
21+
i = 1, j = 0, s = 0, c, p;
2422

25-
for (; i < l; i++){
26-
j = k.length;
27-
while (s !== 3 && j--){
28-
key = k[j];
29-
if (key.indexOf(upStr.charAt(i)) !== -1) {
30-
code = m[key];
31-
if (code !== sdx[s]){
32-
sdx[++s] = code;
23+
while ((c = str.charAt(i++)) && s < 3 ){
24+
j = 0;
25+
while (p = k[j++]){
26+
if (p.indexOf(c) !== -1) {
27+
if (j !== sdx[s]){
28+
sdx[++s] = j;
3329
}
30+
break;
3431
}
3532
}
3633
}
37-
3834
return sdx.join('');
3935
}
36+

0 commit comments

Comments
 (0)