Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for tail call optimization #40

Open
kobezzza opened this issue Jul 10, 2014 · 3 comments
Open

Support for tail call optimization #40

kobezzza opened this issue Jul 10, 2014 · 3 comments

Comments

@kobezzza
Copy link

http://www.2ality.com/2014/04/call-stack-size.html
ECMAScript 6 will have tail call optimization: If a function call is the last action in a function, it is handled via a “jump”, not via a “subroutine call”. That means that, if you slightly rewrote computeMaxCallStackSize(), it would run forever under ECMAScript 6 (in strict mode)

@nmn
Copy link

nmn commented Aug 1, 2014

This is not possible as a transpiler and is something that needs to be added at the javascript engine level.

@kobezzza
Copy link
Author

kobezzza commented Aug 1, 2014

It's possible. Absolutely any recursion can be written in the form Cycle. Tail recursion is optimized very simple, because they don't need the call stack.

http://en.wikipedia.org/wiki/Tail_call

For example LispyScript supports it.

// Tail call recursion

function fac_times(n, acc) {
    return (n == 0) ? acc : fac_times(n - 1, acc * n);
}

function factorial(n) {
    return fac_times(n, 1);
}

// After optimization

function fac_times(n, acc) {
    while (true) {
        if (n == 0) {
            return acc;
        }

        var old_n = n,
            old_acc = acc;

        n = old_n - 1;
        acc = old_acc * old_n;
    }
}

function factorial(n) {
    return fac_times(n, 1);
}

@termi
Copy link
Owner

termi commented Sep 5, 2014

I can do it. Right after generator and modules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants