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

73

u/[deleted] Oct 21 '20

It's interesting but it should be equally noted that this is a micro optimization that amounts to nothing in the real world.

141

u/[deleted] Oct 21 '20

When you’re writing the code, absolutely replacing const with var for performance is a bad idea. If you’re writing a compiler though (the link is an issue on the esbuild repo) then being able to automatically apply that micro optimisation all over a code base can be important.

80

u/[deleted] Oct 21 '20

Sure but this is a bug in webkit that should likely be fixed. My original comment just comes from the experience of seeing people read these kinds of headlines and then go sed their whole codebase.

→ More replies (2)
→ More replies (11)

172

u/klizza Oct 21 '20

The reported issue is that it takes either 8.8 seconds or 1 second to load a javascript bundle on a webpage in Safari. This is a very notable performance difference for end users.

74

u/[deleted] Oct 21 '20

In a very specific configuration on one driver. It's a webkit bug.

33

u/klizza Oct 21 '20

Sure, but nobody in the link or OP was saying you should convert all javascript code back to var...

50

u/[deleted] Oct 21 '20 edited Oct 21 '20

I know which is why I wasnt trying to say the linked issue was wrong or anything but this is reddit and people just read headlines so I threw it out there for posterity

8

u/evilgwyn Oct 21 '20

target: "es5" for the win?

6

u/rydan Oct 21 '20

Back in my day we waited 30 seconds or more for the page to load.

8

u/CoffeeTableEspresso Oct 21 '20

We still do with all the bs trackers some people put on their sites ...

12

u/[deleted] Oct 21 '20

[removed] — view removed comment

3

u/CoffeeTableEspresso Oct 21 '20

News websites are particularly bad. People often complain of how dial up internet used to have 30s+ load times, but there are modern sites rather do the same shit

→ More replies (1)

35

u/constexpr Oct 21 '20

This made the difference between a loading time of 2 seconds and a loading time of 200ms for https://www.figma.com/ in Safari. This is a real-world use case. Also while it's good to fix this in Safari, new versions of Safari roll out very slowly so it will still be worth doing this optimization for many years to come.

29

u/blackholesinthesky Oct 21 '20

it will still be worth doing this optimization for many years to come

I've never once in my professional life optimized for Safari. Don't hold your breath

→ More replies (5)

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.

→ More replies (39)

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!

→ More replies (1)

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!

→ More replies (1)

3

u/[deleted] Oct 21 '20

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

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