MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

add more *advanced* fib benchmarks

+114
+19
examples/demo/fibonacci_iterative.js
··· 1 + function fib(n) { 2 + let a = 0n; 3 + let b = 1n; 4 + 5 + for (let i = 0; i < n; i++) { 6 + const next = a + b; 7 + a = b; 8 + b = next; 9 + } 10 + 11 + return a; 12 + } 13 + 14 + const start = performance.now(); 15 + const result = fib(5000); 16 + const end = performance.now(); 17 + 18 + console.log(`fibonacci(5000) = ${result}`); 19 + console.log(`Time: ${(end - start).toFixed(4)} ms (${((end - start) * 1000).toFixed(2)} ยตs)`);
+12
examples/demo/fibonacci_iterative_tco.js
··· 1 + function fib(n, a = 0, b = 1) { 2 + if (n === 0) return a; 3 + return fib(n - 1, b, a + b); 4 + } 5 + 6 + const start = performance.now(); 7 + const n = 1476; 8 + const result = fib(n); 9 + const end = performance.now(); 10 + 11 + console.log(`fibonacci(${n}) = ${result}`); 12 + console.log(`Time: ${(end - start).toFixed(4)} ms (${((end - start) * 1000).toFixed(2)} ยตs)`);
+42
examples/demo/fibonacci_iterative_tco_double.js
··· 1 + function normalize(pair) { 2 + if (pair[0] === 0) { 3 + pair[1] = 0; 4 + return pair; 5 + } 6 + 7 + while (pair[0] >= 10) { 8 + pair[0] /= 10; 9 + pair[1]++; 10 + } 11 + 12 + while (pair[0] < 1) { 13 + pair[0] *= 10; 14 + pair[1]--; 15 + } 16 + 17 + return pair; 18 + } 19 + 20 + function fib(n, a = new Float64Array([0, 0]), b = new Float64Array([1, 0])) { 21 + if (n === 0) return `${a[0].toPrecision(17)}e+${a[1]}`; 22 + 23 + const next = new Float64Array(2); 24 + 25 + if (a[1] > b[1]) { 26 + next[0] = a[0] + b[0] / 10 ** (a[1] - b[1]); 27 + next[1] = a[1]; 28 + } else { 29 + next[0] = b[0] + a[0] / 10 ** (b[1] - a[1]); 30 + next[1] = b[1]; 31 + } 32 + 33 + return fib(n - 1, b, normalize(next)); 34 + } 35 + 36 + const start = performance.now(); 37 + const n = 5000; 38 + const result = fib(n); 39 + const end = performance.now(); 40 + 41 + console.log(`fibonacci(${n}) ~= ${result}`); 42 + console.log(`Time: ${(end - start).toFixed(4)} ms (${((end - start) * 1000).toFixed(2)} ยตs)`);
+41
examples/demo/fibonacci_iterative_tco_number.js
··· 1 + function normalize(mantissa, exponent) { 2 + if (mantissa === 0) return [0, 0]; 3 + 4 + while (mantissa >= 10) { 5 + mantissa /= 10; 6 + exponent++; 7 + } 8 + 9 + while (mantissa < 1) { 10 + mantissa *= 10; 11 + exponent--; 12 + } 13 + 14 + return [mantissa, exponent]; 15 + } 16 + 17 + function fib(n, am = 0, ae = 0, bm = 1, be = 0) { 18 + if (n === 0) return `${am.toPrecision(17)}e+${ae}`; 19 + 20 + let nm; 21 + let ne; 22 + 23 + if (ae > be) { 24 + nm = am + bm / 10 ** (ae - be); 25 + ne = ae; 26 + } else { 27 + nm = bm + am / 10 ** (be - ae); 28 + ne = be; 29 + } 30 + 31 + const next = normalize(nm, ne); 32 + return fib(n - 1, bm, be, next[0], next[1]); 33 + } 34 + 35 + const start = performance.now(); 36 + const n = 5000; 37 + const result = fib(n); 38 + const end = performance.now(); 39 + 40 + console.log(`fibonacci(${n}) ~= ${result}`); 41 + console.log(`Time: ${(end - start).toFixed(4)} ms (${((end - start) * 1000).toFixed(2)} ยตs)`);