Skip to content

Instantly share code, notes, and snippets.

@frankfaustino
Last active January 6, 2018 18:26
Show Gist options
  • Save frankfaustino/e528833dccf06d17bc83acbec852757b to your computer and use it in GitHub Desktop.
Save frankfaustino/e528833dccf06d17bc83acbec852757b to your computer and use it in GitHub Desktop.
Factorial Calculator (Iterative vs Recursion)
// The for loop performs much faster
function nFactorial (num) {
let factorial = 1;
for (let i = 1; i <= num; i++) {
factorial *= i;
}
return factorial;
}
// Recursion is about 11× slower
const nFactorial = n => n < 0 ? -1 : n === 0 ? 1 : n * nFactorial(n - 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment