forEach has perf problems, please don't use: https://github.com/angular/angular/wiki/performance  Also could you reimplement without recursion so that we don't have to have so many temporary arrays which get re-copied during concatenation. 