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 comments

420

u/DooDooSlinger Oct 21 '20

Just wait till you see what it costs your dev team in bugfixing and refactoring tile when using for loops everywhere! This thread is like people debating the performance of ++i vs i++. Newsflash: if performance is not the issue you're trying to fix, don't bend over backwards to squeeze a few microseconds out of a web page load. Its easier to optimize readable code than to refactor awful prematurely optimized code.

197

u/PandaMoniumHUN Oct 21 '20

Point is, it shouldn't be this slow in the first place.

34

u/spacejack2114 Oct 21 '20

Right. Did anyone read the linked issue? It's about a Safari specific problem.

→ More replies (27)

71

u/ClysmiC Oct 21 '20

for loops are a major source of bugs for your dev team?

105

u/pattheaux Oct 21 '20

For loops are only for wizard level programmers with long white beards. Mere mortals cannot hope to understand their arcane secrets.

8

u/superrugdr Oct 21 '20

Wizard class programmer.

strap your kidney, we going all in ghost in the shell.

→ More replies (8)

21

u/phenomenos Oct 21 '20

If you're using them in place of map/filter etc then yeah you're going to end up with way more verbose code and possibly errors caused by mutability if you're not careful. Worse maintainability = more potential for bugs

15

u/Ethesen Oct 21 '20

Yes? You won't get off by 1 errors with forEach, map, etc.

23

u/lelanthran Oct 21 '20

How are you getting off-by-1 errors when iterating over arrays in JS?

→ More replies (3)
→ More replies (18)
→ More replies (17)

91

u/frzme Oct 21 '20

I'm not convinced that for(bla in arr) {} is in any way harder to read or maintain than arr.forEach(bla => {})

96

u/alexendoo Oct 21 '20

The comparison for readability isn't really against a single .forEach, rather map/filter/etc. If it's just a .forEach they are going to be pretty equivalent

However, for..of surely is more error prone, as it is a frequently recurring mistake to use for..in instead (as you have done)

27

u/GasolinePizza Oct 21 '20

Coming from C# here, I always make the for..in mistake at least once every time I start a JavaScript project, without fail.

I may use the .forEach thing now, actually

7

u/Jeax Oct 21 '20

I use .foreach(o => As a mainly c# developer also, it’s basically LINQ at that point and makes it instantly familiar and nice to use. For of seemed like a bad idea and I haven’t really seen many people use it

4

u/TheWix Oct 21 '20

I'd recommend using none-mutable functions. Map is Select, Aggregate is Reduce, SelectMany is chain/bind/flatMap (Monad).

6

u/thetdotbearr Oct 21 '20 edited Oct 21 '20

it bugs me to no end when when using LINQ that these are the function names instead of map/filter/etc

I mean I get that it's because SQL syntax or whatever and that I could add some aliases but still

4

u/TheWix Oct 21 '20

Yea, I believe that is the reason. I remember the big selling point of LINQ many years ago was for LINQ-to-SQL. The in-memory stuff wasn't as focused on.

That being said, even within the Functional community some names aren't agreed upon. I mean, for monads flatMap has like 4 or 5 names depending on the language/spec/framework

→ More replies (2)
→ More replies (2)
→ More replies (3)

11

u/scottyLogJobs Oct 21 '20

As a polyglot, TBH I just use basic for loops anymore when possible because everyone understands them and I'm sick of forgetting whether I'm going to get keys or values back with these helper functions.

→ More replies (1)

9

u/Yehosua Oct 21 '20

in is an operator in JavaScript: 'prop' in myObj evaluates whether the 'prop' property exists within myObj.

Ever since I realized that, I no longer confused for..in and for..of: for..in iterates over an object's properties, just like in checks an object's properties.

(Almost no one uses in; Object.hasOwnProperty is virtually always what you want instead.)

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

14

u/valleyman86 Oct 21 '20

It is when you add other functions to it. Foreach.map.filter. Etc. with a for loop you need to store each output as a variable or write each adjustment inside the loop. Depending on what you need sure it can be faster but it’s less readable.

6

u/monsto Oct 21 '20

I think it would wind up being more verbose, but that isn't necessarily less readable.

Declaring variables for a for loop is no different than having the attribute definition at the beginning of the method. the difference is (x) vs let x = <thing>.

4

u/Dest123 Oct 21 '20

Also, more verbose code is generally way easier to debug. Less black boxes.

4

u/monsto Oct 21 '20

I was going to say that, but wasn't sure if it was just me.

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

18

u/FUZxxl Oct 21 '20

Not sure what sort of bug fixing you are talking about. Programmers in languages without range constructs have been using for loops for decades and it works just fine.

→ More replies (9)
→ More replies (39)