Test name | Executions per second |
---|---|
My Pow | 63011.0 Ops/sec |
Math.Pow | 534498.2 Ops/sec |
function myPow(x, n) {
function calc_power(x, n) {
if (x === 0) {
return 0;
}
if (n === 0) {
return 1;
}
let res = calc_power(x, Math.floor(n / 2));
res = res * res;
if (n % 2 === 1) {
return res * x;
}
return res;
}
let ans = calc_power(x, Math.abs(n));
if (n >= 0) {
return ans;
}
return 1 / ans;
}
myPow(2, 3)
myPow(5, 0)
myPow(7, 1)
myPow(-3, 4)
myPow(-2, 3)
myPow(0, 5)
myPow(0, 0)
myPow(0, -2)
myPow(1, 100)
myPow(10, 2)
myPow(0.5, 10)
myPow(-2, 10)
myPow(2, -3)
myPow(1.5, 2)
myPow(-3, -2)
myPow(99, 100000000000)
Math.pow(2, 3)
Math.pow(5, 0)
Math.pow(7, 1)
Math.pow(-3, 4)
Math.pow(-2, 3)
Math.pow(0, 5)
Math.pow(0, 0)
Math.pow(1, 100)
Math.pow(10, 2)
Math.pow(0.5, 10)
Math.pow(-2, 10)
Math.pow(2, -3)
Math.pow(1.5, 2)
Math.pow(-3, -2)
Math.pow(99, 100000000000)