I am working on a web application that involves doing a some computation with JavaScript. In a benchmark test, I found Chrome is significantly faster than Firefox in term of execution time. When I run with the built in performance analysis tool in both browsers, I found that Firefox spent a lot of time on this function whereas Chrome does not.
// compute n choose k
function choose(n, k) {
k = Math.min(n - k, k);
let res = 1;
for (let i = 1; i <= k; i++) {
res *= n - k + i;
res /= i;
}
return res;
}
Given that my application will invoke this function in the order of magnitude of 10^7 times each with n <= 40 and k <= 10 during the benchmark, I suspect that Chrome's JavaScript engine will cache the result of this function (since it's deterministic and has no side effect) whereas Firefox does not. However, I am not familiar enough with the internals of those 2 JavaScript engine to be sure. It's appreciated if someone can provide more detailed explanation.
In addition, how should I change my code or browser settings to make it run faster on Firefox?