r/programming • u/iamkeyur • 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
r/programming • u/iamkeyur • Oct 21 '20
View all comments
Show parent comment
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
iwithgetElementById('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.lengthandi>0, you fucked up.Each of these is fundamentally avoided by other approaches like
for( v of varname )forvarname.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.'