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] Replace ‘var’ with ‘let’ everywhere (boost x2) #4255

Closed
ghost opened this issue Apr 15, 2016 · 10 comments
Closed

[CS2] Replace ‘var’ with ‘let’ everywhere (boost x2) #4255

ghost opened this issue Apr 15, 2016 · 10 comments
Labels

Comments

@ghost
Copy link

ghost commented Apr 15, 2016

I made some interesting performance tests. One of them.
Calculation time : 0.570 ms.
I made same test in Babel and it was 2x time faster.
I found bottleneck. I just replaced var with let and got calculation time : 0.235 ms.
As you see i get 2x speed boost only by replacing var with let.

Main problem: 'let' is not supported by old browsers.
Solution: you can make this optional with compiler flag.

So can u make compile option to put let everywhere instead of var ?

@vendethiel
Copy link
Collaborator

vendethiel commented Apr 15, 2016

We don't really do "compilation flags that change the generated code"... Yet. An ES6 mode has been proposed here: #4132.

@shakvaal
Copy link

It's understandable that you are under impression of numbers, but think of what you really suggest: var and let have slightly different behavior, so simply changing one for another would lead to numerous bugs and break everything.

@ghost
Copy link
Author

ghost commented May 27, 2016

var and let have slightly different behavior, so simply changing one for another would lead to numerous bugs and break everything.

Could you explain it more detail in context of coffeescript usage?
As i know in coffeescript all of variable declarations are pushed up to the top of the closest scope so difference should be minimal.

@vendethiel
Copy link
Collaborator

It's not in the case of for loops.

@connec
Copy link
Collaborator

connec commented May 28, 2016

It looks like CS already hoists variable declarations in loops.

for a in b
  c = 1
# var a, c, i, len;
#
# for (i = 0, len = b.length; i < len; i++) {
#   a = b[i];
#   c = 1;
# }

@trylks
Copy link

trylks commented Aug 3, 2016

This is not my field of expertise, but I would like to point one little thing. If an improvement for this is devised, perhaps const could be considered along with let. I have not made any numbers, but JavaScript interpreters should be much faster with const, in the future if not in the present.

In theory, since the scope of the variables is limited, it should be feasible to check statically if they are assigned once or more than once, to automatically specify const where possible (no changes to CoffeeScript syntax). I know that Eclipse has an option to automatically add final where possible. Something similar might be possible here.

I do not want to derail the current discussion. Should I open a new issue?

@ghost
Copy link
Author

ghost commented Aug 4, 2016

I do not want to derail the current discussion. Should I open a new issue?

If I have correctly understood your theory is not based on v8 sources nor performance tests.
Also automatically specifing const where possible does not sound as easy to implement as var to let swap.
So probably yes, you should create new issue.

@Inve1951
Copy link
Contributor

replacing all var to let seems to only not work on the repl and could be implemented on the 2 branch.
cs6/discuss#35

@GeoffreyBooth, this proposal might interest you

@GeoffreyBooth
Copy link
Collaborator

I’m sure we could fix the REPL to work with let. I just updated the let branch I mentioned in my comment that you linked to, with the latest updates from the 2 branch; now it has several more failing tests. So there’s more work to be done than just fixing the REPL, but it’s probably not too drastic.

@Inve1951, perhaps you’d like to take on the task of getting the let branch to pass all the tests? And then if there really is a measurable performance boost in real-world use cases, it’s probably worth doing. Since Node supports let natively, and plenty of CoffeeScript is intended for a server-side Node environment, there could be real benefits for that use case.

@GeoffreyBooth GeoffreyBooth changed the title Replace 'var' with 'let' everywhere (boost x2) [CS2] Replace ‘var’ with ‘let’ everywhere (boost x2) Jan 23, 2017
@GeoffreyBooth
Copy link
Collaborator

This was thoroughly discussed in coffeescript6/discuss#35 and coffeescript6/discuss#58. The findings:

  • Modern JavaScript runtimes already optimize things such that there shouldn’t be a noticeable performance benefit between var, let and const in real-world code.
  • Simply replacing var where it appears now with let in the same places, to create a “function-scoped let” like in my experimental let branch, should offer zero performance benefits. The benefit would come if the declarations were made more narrowly, at the block level, not at the function level where our var declarations are now.
  • One of CoffeeScript’s goals is outputting human-readable, sensible code. Outputting this “function-scoped let“ violates that principle. let implies that the variable it’s declaring is a block-scoped variable. Function-scoped variables should be declared with var. That’s what var is for.

Based on the last point, I think function-scoped variables will always be declared with var for the foreseeable future. If CoffeeScript ever adds support for block-scoped variables (and based on coffeescript6/discuss#58 it looks unlikely), that’s when let would be used.

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

No branches or pull requests

6 participants