cujojs / when

A solid, fast Promises/A+ and when() implementation, plus other async goodies.
Other
3.44k stars 396 forks source link

Improve reduce and reduceRight perf #391

Closed briancavalier closed 9 years ago

briancavalier commented 9 years ago

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

briancavalier commented 9 years ago

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.