facebookarchive / prepack

A JavaScript bundle optimizer.
http://prepack.io
Other
14.22k stars 425 forks source link

Is it possible to convert recursive method to iterative? #2633

Closed Beraliv closed 5 years ago

Beraliv commented 5 years ago

I have a code like:

const getNumberOfParents = (element: HTMLElement) => {
    return element.parentElement === null
        ? 0
        : 1 + getNumberOfParents(element.parentElement);
}

I want to convert it to iterative automatically. Is it possible to do that with prepack?

I know it's the task for the engine like V8 but is it possible to speed up old browsers with it?

like:

const getNumberOfParents = (element: HTMLElement) => {
    let currentElement = element;
    let count = 0;
    while (element.parentElement !== null) {
        count++;
        currentElement = currentElement.parentElement;
    }
    return count;
}
NTillmann commented 5 years ago

I don't think that Prepack would be the best tool for this task, as Prepack would have to be able to understand the meaning of the entire body,. even though the only thing that really matters is identifying the recursive structure.

Beraliv commented 5 years ago

Sad Thank you anyway