This uses recursion (which will be trampolined--no call stack concerns), and is 2-3x faster than the previous version which used Array.prototype.reduce. The code is a lot uglier than it should be due to having to deal with sparse arrays. At least half the new code is for sparse arrays :( Yet another reason to drop sparse array support in 4.0.
Here's a comparison of what the algorithm looks like without sparse array support. Nearly half the size :( ... can't wait to drop sparse array support in 4.0.
This uses recursion (which will be trampolined--no call stack concerns), and is 2-3x faster than the previous version which used Array.prototype.reduce. The code is a lot uglier than it should be due to having to deal with sparse arrays. At least half the new code is for sparse arrays :( Yet another reason to drop sparse array support in 4.0.
See example of the difference here: https://gist.github.com/briancavalier/e080a3ddced89c1ece84