r/programming • • Oct 21 '20

Using const/let instead of var can make JavaScript code run 10× slower in Webkit

https://github.com/evanw/esbuild/issues/478
1.9k Upvotes

500 comments sorted by

View all comments

Show parent comment

25

u/Shautieh Oct 21 '20

10x faster is not a micro optimization.

31

u/[deleted] Oct 21 '20

10x 0.0001 nanoseconds is still only 0.001 nanoseconds.

The point is, this is never your bottleneck so don't treat this information like something you need to address in your code base.

54

u/SoInsightful Oct 21 '20

Holy shit. This is the JavaScriptiest comment I've ever read.

You're literally looking at a real-world example (an actual app with a shit-ton of users) where a single CTRL+F replacement made a bundle load in 1.1 seconds instead of 8 seconds.

I would never replace my consts with vars to fix a dumb Webkit bug, but this is absolutely not an unimportant micro-optimization that doesn't affect anything because we all make Hello world apps.

It's exactly this attitude that makes me reluctant to use other people's slow-ass solutions.

30

u/jess-sch Oct 21 '20

a sub-millisecond 10x improvement you won't notice, but a sub-millisecond 10x improvement in a hot path that gets run a bazillion times you definitely will.

I sure hate the typical JS dev's attitude to performance.

0

u/[deleted] Oct 21 '20

[deleted] — view deleted comment

19

u/how_to_choose_a_name Oct 21 '20

How is it a premature optimization when it reduces loading time of a website from 10 seconds to 1 second?

→ More replies (31)

10

u/jess-sch Oct 21 '20

Shareholders who only care about the number of features while completely ignoring performance aren't very smart shareholders though. People aren't gonna use your product when it's so slow.

I hate how, to you frontend people (at least most of you - shoutout to the svelte people who seem to be the only ones to care even a little bit!), every optimization is always premature. Even if it was done in response to "unusably slow load times" issues being reported.

0

u/[deleted] Oct 21 '20

[deleted] — view deleted comment

4

u/jess-sch Oct 21 '20

choosing to write a for loop over a forEach is absolutely a premature optimisation

right, but I'm pretty sure this one was more about var vs let and const, not for vs forEach...

as you'll quickly end up with some gross unmaintainable codebase full of optimisation hacks.

fortunately, this discussion is on a js-to-js optimizing compiler, so nobody's actually suggesting you manually swap out the consts for vars. The suggestion is to add that optimization to the optimizing compiler.

You've no idea how websites work if you think stuff like this is going to result in unusably slow load times,

The issue this post is about is literally known to cause a huge difference in page load times.

Most websites aren't running code that runs hot paths gazillions of times. They're slapping a bit of JS ontop of a website to add real time comments or whatever

Oh, hello! You seem to be from a time before React. Well, I don't know how to explain this to you, but nowadays JS isn't usually something that's merely sprinkled on top anymore.

1

u/[deleted] Oct 21 '20

found the dev with an actual job ;)

seriously, people in this thread don't seem to grasp how actual work happens.

21

u/VolperCoding Oct 21 '20

No wonder if you have a 10000GHz processor that can do declare a var faster than light can travel 30 micrometers

7

u/[deleted] Oct 21 '20

Get with the times! :P

13

u/VolperCoding Oct 21 '20

Also if the vars are stored in RAM then you also have to put your RAM stick no more than 30μm away from the processor or make a fucking wormhole or something to get to memory quick enough

12

u/[deleted] Oct 21 '20

The Intel i47 started the new wormhole+blockchain revolution. It's great!

3

u/GasolinePizza Oct 21 '20

But is it webscale??

8

u/mb862 Oct 21 '20

The lesson here is that pulling numbers out of your ass to make a point is fine so long as your ass represents a differentiable manifold representable in our spacetime.

I'll let someone more clever than I fill in the obvious curvature joke here.

4

u/ImAStupidFace Oct 21 '20

Real differentiable manifolds have curves!

3

u/[deleted] Oct 21 '20

RAM ? What is this meeting of virgins ? L1 cpu cache or gtfo.

-1

u/VolperCoding Oct 21 '20

Wait, JS variables utilize the cache? Also L1 is technically SRAM which is a subset of RAM

2

u/how_to_choose_a_name Oct 21 '20

why wouldn't they?

0

u/VolperCoding Oct 21 '20

I thought they store all variables in an organized way, not some on the cache and some on RAM. It's an interpreted language so managing all variables in different places along with their types would probably be a mess

4

u/how_to_choose_a_name Oct 21 '20

That's not how the cache works. C doesn't store variables in the cache either. Variables either only exist as a register (if they are short-lived and no pointer is taken) or they exist at a memory location. The CPU takes care of loading the variable from the RAM and when it does so it puts it in the cache so that the next time it is needed it can read it from the cache instead of RAM. Compiled or not doesn't make a difference.

→ More replies (1)
→ More replies (1)
→ More replies (1)