As the title says I'm searching for the most efficient way to create a flat array output from a recursive function.
The following example produces the correct result but it is kind of slow as it has to create a temporary concatenated array of points for each level of recursion.
class QuadTree {
// ...
// some other methods
// ...
get points() {
if (this.divided) {
// concatenate and return points of all subtrees
return this.subtrees[0].points.concat(
this.subtrees[1].points,
this.subtrees[2].points,
this.subtrees[3].points
);
}
// return _points array of this quadtree
return this._points;
}
}
Is there a way to speed this up?
I would think that this kind of problem is quite common when working with nested/treelike datastructures but I have not found a satisfying solution.