Skip to content

Instantly share code, notes, and snippets.

@johnhaley81
Last active March 18, 2021 22:38
Show Gist options
  • Save johnhaley81/5d4940aba4d23611393ca40b1ce49d0e to your computer and use it in GitHub Desktop.
Save johnhaley81/5d4940aba4d23611393ca40b1ce49d0e to your computer and use it in GitHub Desktop.
Factorial promise/generator test
function factorialCallback(acc, n, callback) {
if (n === 0) {
callback(acc);
} else {
setImmediate(() => factorialCallback(acc * n, n - 1, callback));
}
}
function timedFactorialCallback(n) {
const start = new Date();
const result = factorialCallback(1, n, () => console.log(`factorialCallback finished in: ${new Date() - start}`));
return result;
}
timedFactorialCallback(100000);
function* Factorial() {
var n = 1, total = 1;
while (true) {
total = total * n++;
yield total;
};
}
function factorialGenerators(n) {
var f = Factorial(), k, nf;
for (k = 0; k < n; k += 1) {
nf = f.next().value;
}
return nf;
}
function timedFactorialGenerators(n) {
const start = new Date();
const result = factorialGenerators(n)
console.log(`factorialGenerators finished in: ${new Date() - start}`);
return result;
}
timedFactorialGenerators(100000);
function* FactorialTryCatch() {
var n = 1, total = 1;
while (true) {
try {
total = total * n++;
yield total;
} catch (e) {
console.log(e);
}
};
}
function factorialGeneratorsTryCatch(n) {
var f = FactorialTryCatch(), k, nf;
for (k = 0; k < n; k += 1) {
nf = f.next().value;
}
return nf;
}
function timedFactorialGeneratorsTryCatch(n) {
const start = new Date();
const result = factorialGeneratorsTryCatch(n)
console.log(`factorialGeneratorsTryCatch finished in: ${new Date() - start}`);
return result;
}
timedFactorialGeneratorsTryCatch(100000);
function factorialPromises(n) {
var start = Promise.resolve(n);
while (n -= 1) {
start = start.then(function (n, value) {
// console.log('n', n, 'value', value)
return value * n;
}.bind(null, n));
}
return start;
}
function timedFactorialPromise(n) {
const start = new Date();
const result = factorialPromises(n);
result.then(() => console.log(`factorialPromises finished in: ${new Date() - start}`));
return result;
}
timedFactorialPromise(100000);
johnhosx :: ~ » node factorialCallbackTest.js
factorialCallback finished in: 187
johnhosx :: ~ » node factorialGeneratorTryCatchTest.js
factorialGeneratorsTryCatch finished in: 46
johnhosx :: ~ » node factorialGeneratorTest.js
factorialGenerators finished in: 44
johnhosx :: ~ » node factorialPromisesTest.js
factorialPromises finished in: 406
johnhosx :: ~ » node -v
v6.6.0
johnhosx :: ~ » nvm use 6.5
Now using node v6.5.0 (npm v3.10.3)
johnhosx :: ~ » node factorialCallbackTest.js
factorialCallback finished in: 171
johnhosx :: ~ » node factorialGeneratorTryCatchTest.js
factorialGeneratorsTryCatch finished in: 53
johnhosx :: ~ » node factorialGeneratorTest.js
factorialGenerators finished in: 52
johnhosx :: ~ » node factorialPromisesTest.js
factorialPromises finished in: 416
johnhosx :: ~ » node -v
v6.5.0
johnhosx :: ~ »
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment