r/javascript • • Apr 30 '16

question How much performance does using const with ES6 gain you?

It seems that the general consensus of people writing ES6 is to use const for variables that won't change in value, which principally I can't disagree with.

But sometimes I wonder how much that really gains your application from a performance standpoint. If someone tries to reassign a const somewhere in the app that truly shouldn't be reassigned, the value there is clear. But if we're talking in terms of pure processing speed, does it make a difference?

Let's say in the worst-case scenario, I replaced all instances of const in your app with let (or even var), and run a million iterations of the most complex function affected by this. What would change? Would it run any slower? Any difference for server-side code vs. client-side?

Just wondering this from a curiosity standpoint. I'm not against const in any way, would just like to know what it gains you besides the type of bug prevention it can possibly give in other programming languages.

11 Upvotes

15 comments sorted by

View all comments

-4

u/saadq_ Apr 30 '16

I don't know about performance but I've started to get annoyed with having to type the five letter const everywhere. I wish they had used something like val instead. I've been considering just saying screw it and using let everywhere instead.

5

u/[deleted] Apr 30 '16

This is such a non-issue for me that I can't even comprehend how it would be a problem for a programmer. Do you type with just one finger? What's your WPM?

I'm not trying to be insulting, this is actually kind of blowing my mind. The difference between typing out these two keywords is so minor, if you're using any sort of correct typing technique, that it barely even saves you a millisecond. This is like C programmers trying to convert every variable into some obscure unreadable contraction instead of typing out what the variable does. You're sacrificing a significant improvement of readability in order to save 0.000000001% of time in your day.

I mean jesus, if this is such a problem, get a text editor that allows you to type snippets. Then you can press something like c+enter and "const" will be outputted. Now you only have to press 2 letters instead of the 3 for "let".

1

u/jsaele Apr 30 '16

He'll probably end up spending more time in total when he comes back to the code and it takes way more time to read than if he wrote it with intent and readability in mind :)

1

u/va9iff Jan 26 '23

or maybe he wants it to be 3 letter so 3 + 1 space = 4 and may look nicer if he uses 4 space for tabs :D

-2

u/azium Apr 30 '16

I made a similar choice and have been sticking with let, particularly on personal stuff. My mantra is to never mutate whenever possible, and as such glancing at the way a variable is declared doesn't change my opinion about how its used.