Last active
July 17, 2017 19:36
-
-
Save djleonskennedy/43c3731b52d17e6a712d0bdeabe76aa7 to your computer and use it in GitHub Desktop.
recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Recursive: | |
const factorial = n => | |
n < 2 | |
? 1 | |
: n * factorial(n-1); | |
// Tail-recursive: | |
const factorial = n => { | |
const _tempFactorial = (n, acc) => | |
n < 2 | |
? acc | |
: _tempFactorial(n-1, n * acc) | |
return _tempFactorial(n, 1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
some tests: https://jsperf.com/tail-over-regular-recursion