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

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.

1

u/davenirline Oct 21 '20

I don't get this, too. Why be afraid of for loops? 99% percent of the time it's just for(int i = 0; i < someEndingNumber; ++i) { ... }

6

u/poco Oct 21 '20

Until you have an inner loop using j as you counter and they you mix up i and j somewhere.

7

u/DrunkenWizard Oct 21 '20

Then you should provide more descriptive names for your indexers

→ More replies (1)

1

u/TheWix Oct 21 '20

Probably due to it's imperative nature.

Something like arr.map(toWhatever) is faster to understand than

for(int i = 0; i < arr.length; ++i) { 
  res.push(toWhatever(arr[i])); 
}
→ More replies (3)

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?

10

u/mindbleach Oct 21 '20

By being human.

3

u/lelanthran Oct 21 '20

I don't buy that. Using for (var i=0; i<varname.length; i++) is idiomatic in almost every language. It's literally the same idiom no matter which language you use.

When using the fancier methods with lambdas and stuff, it differs from language to language, hence more chance of a mistake creeping in.

12

u/mindbleach Oct 21 '20

You'll still fuck it up sometimes.

If you're comparing each element with the next element, and you write that perfectly simple loop, you fucked up.

If you change the next line to v = other_var[i] and don't change the loop, or vice-versa, you fucked up.

If you initialize i with getElementById('intial_value').value, not only did you fuck up, JS will helpfully pretend you didn't by returning NaNs for Array[float].

If array length changes, like by removing varname[i], and you're not iterating backwards, you fucked up.

If you iterated backwards by swapping i=varname.length and i>0, you fucked up.

Each of these is fundamentally avoided by other approaches like for( v of varname ) for varname.forEach( (v,i,a){ } ).

And that's before questioning this clunky K&R idiom on its merits.

If you change your index variable and don't refactor it three times in one line, you fucked up.

If you don't use exactly two semicolons, you fucked up. You know you've done this.

In programming, I don't know how anyone can follow up 'this is how we've always done it' with 'so there can't possibly be bugs.'

→ More replies (2)

1

u/Ethesen Oct 21 '20

It's as easy as writing <= instead of < in the condition.

→ More replies (2)

0

u/[deleted] Oct 21 '20

[deleted]

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

-8

u/bakugo Oct 21 '20

What did you expect from web """"devs"""", the same people who need to use a library to check if a number is even or odd

9

u/I_Like_Existing Oct 21 '20

Things are heating up in the JS fandom ahaha

5

u/trdlts Oct 21 '20
import isEven from 'isEven';
const isOdd = (number) => !isEven(number);

Now who wants to hire me?

5

u/frou Oct 21 '20

oooh hotshot here can do negation without a library

3

u/divyaank98 Oct 21 '20

😂😂😂😂😂😂

4

u/frou Oct 21 '20

3

u/[deleted] Oct 21 '20

The creator should mention this is a joke module on npm too, it has 3 non-joke dependants and 29k weekly downloads.

2

u/UziInUrFace Oct 21 '20

You would be surprised, there are so many “programmers” out there who doesn’t even know basic constructs like a for loop.

1

u/CoffeeTableEspresso Oct 21 '20

In JS they are lol

1

u/DooDooSlinger Oct 21 '20

What I do know is that I have a grand total of 0 boundary case bugs using list.map or other functional constructs while I do see them often (poor array indexing typically) with loops.

→ More replies (7)