Skip to content

Commit c82de57

Browse files
committed
perf: fast-path Math.pow when exponent is 1 in computeScore
When weight and norm are both 1 (common case with default options), skip the Math.pow call and use the score directly.
1 parent ec9b446 commit c82de57

11 files changed

Lines changed: 24 additions & 14 deletions

dist/fuse.basic.cjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,9 @@ function computeScoreSingle(matches, {
901901
score
902902
} = matches[i];
903903
const weight = key ? key.weight : null;
904-
totalScore *= Math.pow(score === 0 && weight ? Number.EPSILON : score, (weight || 1) * (ignoreFieldNorm ? 1 : norm));
904+
const s = score === 0 && weight ? Number.EPSILON : score;
905+
const exponent = (weight || 1) * (ignoreFieldNorm ? 1 : norm);
906+
totalScore *= exponent === 1 ? s : Math.pow(s, exponent);
905907
}
906908
return totalScore;
907909
}

dist/fuse.basic.min.cjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/fuse.basic.min.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/fuse.basic.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,9 @@ function computeScoreSingle(matches, {
899899
score
900900
} = matches[i];
901901
const weight = key ? key.weight : null;
902-
totalScore *= Math.pow(score === 0 && weight ? Number.EPSILON : score, (weight || 1) * (ignoreFieldNorm ? 1 : norm));
902+
const s = score === 0 && weight ? Number.EPSILON : score;
903+
const exponent = (weight || 1) * (ignoreFieldNorm ? 1 : norm);
904+
totalScore *= exponent === 1 ? s : Math.pow(s, exponent);
903905
}
904906
return totalScore;
905907
}

dist/fuse.cjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1296,7 +1296,9 @@ function computeScoreSingle(matches, {
12961296
score
12971297
} = matches[i];
12981298
const weight = key ? key.weight : null;
1299-
totalScore *= Math.pow(score === 0 && weight ? Number.EPSILON : score, (weight || 1) * (ignoreFieldNorm ? 1 : norm));
1299+
const s = score === 0 && weight ? Number.EPSILON : score;
1300+
const exponent = (weight || 1) * (ignoreFieldNorm ? 1 : norm);
1301+
totalScore *= exponent === 1 ? s : Math.pow(s, exponent);
13001302
}
13011303
return totalScore;
13021304
}

dist/fuse.min.cjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/fuse.min.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/fuse.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,7 +1294,9 @@ function computeScoreSingle(matches, {
12941294
score
12951295
} = matches[i];
12961296
const weight = key ? key.weight : null;
1297-
totalScore *= Math.pow(score === 0 && weight ? Number.EPSILON : score, (weight || 1) * (ignoreFieldNorm ? 1 : norm));
1297+
const s = score === 0 && weight ? Number.EPSILON : score;
1298+
const exponent = (weight || 1) * (ignoreFieldNorm ? 1 : norm);
1299+
totalScore *= exponent === 1 ? s : Math.pow(s, exponent);
12981300
}
12991301
return totalScore;
13001302
}

dist/fuse.worker.min.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

dist/fuse.worker.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,9 @@ function computeScoreSingle(matches, {
12781278
score
12791279
} = matches[i];
12801280
const weight = key ? key.weight : null;
1281-
totalScore *= Math.pow(score === 0 && weight ? Number.EPSILON : score, (weight || 1) * (ignoreFieldNorm ? 1 : norm));
1281+
const s = score === 0 && weight ? Number.EPSILON : score;
1282+
const exponent = (weight || 1) * (ignoreFieldNorm ? 1 : norm);
1283+
totalScore *= exponent === 1 ? s : Math.pow(s, exponent);
12821284
}
12831285
return totalScore;
12841286
}

0 commit comments

Comments
 (0)