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

[CS2] Optimize do (...args) -> blocks to use let #4514

Closed
dead-claudia opened this issue Apr 18, 2017 · 7 comments
Closed

[CS2] Optimize do (...args) -> blocks to use let #4514

dead-claudia opened this issue Apr 18, 2017 · 7 comments

Comments

@dead-claudia
Copy link

Blocks with let implicitly create closures, so you could optimize non-global do blocks to use let rather than IIFEs to generate smaller, cleaner code.

@connec
Copy link
Collaborator

connec commented Apr 19, 2017

@isiahmeadows do you have some example current vs. desirable output?

@vendethiel
Copy link
Collaborator

We need to make sure do (i) still works the same.

@dead-claudia
Copy link
Author

@vendethiel That was implied. I was just referring to do (i) -> ...

@dead-claudia dead-claudia changed the title [CS2] Optimize do blocks to use let [CS2] Optimize do (...args) -> blocks to use let Apr 20, 2017
@GeoffreyBooth
Copy link
Collaborator

Can you please provide examples?

@dead-claudia
Copy link
Author

Here's an example:

# CoffeeScript
res = do (i = 0) ->
    while i < len
        i += 1 if someCondition((s) -> i < s)
        i += 1
    i
// Current JS
var res;
res = (function (i) {
    while (i < len) {
        if (someCondition(function (s) { return i < s })) i += 1;
        i += 1;
    }
    return i;
})(0);

// Proposed JS
var res;
{
    let i = 0;
    while (i < len) {
        if (someCondition(function (s) { return i < s })) i += 1;
        i += 1;
    }
    res = i;
}

(I know the ability to expand functions like that exists, because of how if/else and for already expand similarly and recursively in non-returning locations).

@GeoffreyBooth
Copy link
Collaborator

Basically this is a refactor of do that drops the function scope, and uses block scope instead with variables declared by let to achieve the same effect. Sure, I guess. It’s more idiomatic ES, I suppose, but it’s not like the current JS is so unreadable.

If you’d like to implement this as a PR I suppose it would be an improvement. I’m not going to keep the issue open though because I don’t think this is something the maintainers would prioritize for one of us to tackle; though if you want to give it a shot, by all means please submit a PR.

@connec
Copy link
Collaborator

connec commented May 6, 2017

I think this is pretty interesting, especially in the context of #2518.

Something to consider though is that do can also be used for function application:

something = -> alert 'woah'
do something

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

No branches or pull requests

4 participants