3 ms·
I assumed since this seems to be testing function call overhead, rather than the math, that an equivalent function with BigIntegers would be about the same for
by akubera 6y ago
I assumed since this seems to be testing function call overhead, rather than the math, that an equivalent function with BigIntegers would be about the same for JS. But I tried it just for fun:
const { performance } = require('perf_hooks');
function fib(n) {
if (n == 0 || n == 1) { return 1; }
return fib(n - 1) + fib(n - 2);
}
function fibn(n) {
if (n == 0n || n == 1n) { return 1n; }
return fibn(n - 1n) + fibn(n - 2n);
}
var t0 = performance.now(); fib(35); console.log("fib:", performance.now() - t0);
var t0 = performance.now(); fibn(35n); console.log("fibn:", performance.now() - t0);
results:
fib: 127.30008998513222
fibn: 2134.1405459940434
yikes, I think you're right. (nodejs v15.6.0)
(An equivalent version with Python on my machine: ~2657.3ms)