-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ref: prefer for..of loops over indexed access #84307
base: master
Are you sure you want to change the base?
Conversation
this plays better with `noUncheckedIndexedAccess`, as it avoids a no-null-assertion (!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧹
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice cleanup! 🥔🥔🥔
We need keep profiling to still using the old for loop as that is very performance sensitive part of the codebase (I can fix it after the merge too). The reason is that for of loops have extra iterator overhead that might end up negatively impacting the flamegraph import, stack iteration and a lot of our rendering code which can easily run in the magnitude of 100k loops per second across the render pipeline and with the addition of continuous profiling, the likeliness that we'll end up working with even more in the future is even higher :/ I would also say that I'm generally not entirely on board with this (my philosophy is to just let people write code, as long it's safe) and I think this change might result in a slightly more subtle debugging experience at the expense of convenience. For example, if you consider a user that is investigating a for(let i = 0; ...){
const item = arr[i]! // <- unsafe
doSomething(item)
} vs for(item of arr){
doSomething(item)
} That said, the nice side benefit of this is that we will be warned about non iterables, which I think is a good improvement. I wanted to highlight the small change, but imo my concern isn't strong enough to warrant blocking this. |
This PR refactors traditional for loops which use an indexed access to
for..of
loops, if the index was only used to access the item of the collection that was iterated over.for..of
loops plays better withnoUncheckedIndexedAccess
, as they avoids a no-null-assertion (!)The prefer-for-of rule of
typescript-eslint
was also enabled.Note: These changes should be mostly safe. There’s a slim chance that we were looping over a non-iterable, like an object that had a
length
property. In one case, the types disallowedfor..of
because the element is not aniterable
, only anindexable
. Iterating over a non-iterable will yield a runtime error.IF we had something that was
any
on type level, or was asserted to be an array, when it was in fact anindexable
at runtime, we would now get an error withfor..of
.