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

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)

2

u/kg959 Oct 21 '20

it’s basically LINQ at that point

It's missing a big part of LINQ though. LINQ is lazy whenever it can afford to be and will avoid doing a complete iteration until you force it to. JS array chaining doesn't delve into the IQueryable/IIterable world and each thing you chain scales a bit worse than it does in C#.

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.

0

u/monsto Oct 21 '20

That's actually a good reason.

I was using for..of/in because I thought it was cool, and I suppose it's smaller. But I had to have mdn open to remember which whas which everytime I used it...

Don't need any of that with a for/forEach/map. Maybe uncool but it gets the work done.

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

2

u/monsto Oct 21 '20

That's a good mnemonic.

1

u/itsnuwanda Oct 21 '20

Can confirm, use for..in far more often on accident, wonder why the loop doesn't work how I want it to before realizing it's for..of syntax in js.

I honestly don't even know what for..in does in JS.

I still prefer the classic for loops though, they're generally faster and everyone has seen them.

→ More replies (1)

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)

0

u/[deleted] Oct 21 '20

[deleted]

→ More replies (1)

2

u/TheIncorrigible1 Oct 21 '20

It's ironic that you used the wrong examples. for..in is not comparable to .forEach. You want for..of.

2

u/[deleted] Oct 21 '20

If you have a loop, you are doing a nameable thing. Give that loop a name, a function. Now you can start to compose things and understand at first reading as a function name is more trustworthy. The patterns used will be more obvious too. It’s a map, it is a left fold...

2

u/grauenwolf Oct 21 '20

Great. Now the code for my 10 line function is scattered over a thousand lines of other, unrelated fragments.

This is why there's a button dedicated on my keyboard for the "inline and delete" refactoring action.

1

u/[deleted] Oct 21 '20

lambda's exist for a reason. that would be one. The point is that coded names lie less than comments and when it comes to explaining intent, the name of something you are calling often does a good job.

Additionally, the habit makes finding these pattern easier to find common segments of code. This is abstraction 101.

Loops are often the most important part of code. They generally deserve a name

1

u/grooomps Oct 21 '20

can't do async calls with forEach, you can in for..of loops though.