I have something like:
[ {}, {children:[{}, {}]} , {} ] #each element cane be any level deep
and want to iterate over it without recursion. Just to test performance benefits. Any help?
I have something like:
[ {}, {children:[{}, {}]} , {} ] #each element cane be any level deep
and want to iterate over it without recursion. Just to test performance benefits. Any help?
Every resursive function can be shortcutted into a function, that keeps its own stack - the question is: Is it faster? I guess not.
What I mean here is something like (in pseudo-code)
function flatten(something) {
var ping=[];
var pong=[];
repeat {
if (ping is empty) {
if (something is empty) break;
else ping.prepend(something.shift());
}
var element=ping.shift();
if (element has children)
foreach (child of element in reverse order) ping.prepend(child);
else
pong.append(element);
}
return pong;
}
calling flatten(your_input_object) will result in a "naive humanlike enumeration" list of its entries