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

15

u/Meefims Apr 30 '16

Performance-wise, if whatever difference between let and const actually surfaces with meaningful impact then you're probably already in a very good spot.

const, however, is great for code readability because it is a statement of how the code is supposed to work. If I see a const variable I expect it not to be reassigned. If I see a let, I expect it to be reassigned. Anything else tells me there is likely a bug. Given that code clarity is almost always the greatest concern, using const as the default choice and let only when necessary makes a lot of sense to me.

4

u/Cody_Chaos Apr 30 '16 edited Apr 30 '16

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.

This is true. And it's a fairly good idea. (The benefits can be overstated. Keep in mind that const does not prevent, nor is it intended to prevent, people mutating complex values. const user = getUser(); user.name = 'Tom' is perfectly legal.)

But sometimes I wonder how much that really gains your application from a performance standpoint.

Absolutely, literally, nothing. That's not why people recommend it. I'd say that the benefits of const are basically 100% that it makes the code easier to write and (especially maintain) via marginal improvements to clarity and programmer intent. let tells me that the author is intending the re-assign a variable, which is good to know since that's pretty unusual. const tells me it's a "normal" variable that shouldn't be re-assigned, and my linter will helpfully flag it should I start to try (or if I try and re-declare it). Every so often I'll go "oh right, I already have a user object", and that's slightly helpful, and has no drawbacks, so...win?

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?

Swap with let, and nothing would change at all; it certainly wouldn't run slower. Swap with var, and I suppose the different scoping might cause a bug and due to a couple of minor bugs in some browsers, it might even run faster.

2

u/dmethvin Apr 30 '16

Performance is generally not a good reason to use ES6 features. Use them because they make your code easier for people to understand. Using const tells the reader (and the compiler) that you won't change the binding later, which is handy for a lot of reasons. Right now though, JS engines don't always take advantage of ES6 features to make faster code. To answer your specific question, this test shows that let and const are pretty much the same performance as var.

1

u/natziel Apr 30 '16

If you're still wondering about performance, there are a lot of optimizations that the compiler can make if it knows that data is immutable and functions are pure, which is a reason why Haskell is such a fast language compared to other high level languages.

Realistically, we'll be lucky to see this ever materialize in JavaScript. The performance difference is going to be negligible for a long time, so don't worry about it. Use ES6 (and beyond) features because they improve how you read and write your code, not because of performance.

1

u/Pyrolistical Apr 30 '16

Found the guy who will use classes over factory functions because they are "faster"

Kidding aside code is for humans to read more than computers to run fast. Const and factory functions help in that regard and is why we don't care about performance of them up front. You should care about performance when you have a measurable impact to your problem at hand

0

u/RnRau Apr 30 '16

There are issues with const and let in Chrome from a performance point of view. See the following links for details;

1

u/RnRau Apr 30 '16

I'm curious as to why the above information is wrong? Otherwise, why the downvotes?

1

u/Retrofire-Pink Nov 19 '21

just reddit being reddit

2

u/RnRau Nov 19 '21

Thanks mate!

1

u/Retrofire-Pink Nov 20 '21

glad they removed the archive feature so i could explain that to people 5 years after submission

needs to be said. nuf' said

-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.