Public
Edited
Jan 2, 2024
2 stars
Insert cell
Insert cell
Insert cell
Insert cell
function count(i, max) {
if (i === max) return i;

return count(i + 1, max);
}
Insert cell
Insert cell
count(1, 100_000)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// 1. add async keyword
async function countAsync(i, max) {
if (i === max) return i;

// 2. add the following line
await Promise.resolve();
return countAsync(i + 1, max);
}
Insert cell
Insert cell
await countAsync(1, 100_000)
Insert cell
Insert cell
Insert cell
fibonacci = async (n) => {
const fibo = async (a, b, n) => {
if (n < 1) {
return b;
}

await Promise.resolve();

return fibo(b, a + b, n - 1);
};

return fibo(BigInt(0), BigInt(1), n - 1);
}
Insert cell
await fibonacci(50_000)
Insert cell
Insert cell
Insert cell
Insert cell
/**
* @param {(x: bigint) => void} cb
* @returns {void}
*/
function fibo(a = 0n, b = 1n, n = 0, cb) {
if (n < 1) {
return cb(b);
}

queueMicrotask(() => {
fibo(b, a + b, n - 1, cb);
});
}
Insert cell
/**
* @param {number} n
* @param {(x: bigint) => void} callback
* @returns {void}
*/
fibonacciFast = (n, callback) => {
fibo(BigInt(0), BigInt(1), n - 1, callback);
}
Insert cell
await new Promise((resolve) => {
fibonacciFast(50_000, (x) => {
resolve(x);
});
})
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more