in general a while loop is the fastest
Your sample code moves the temporary objects to the top level which exaggerates the difference.
but when you need to return an array instead its slows the function down.
There is no reason to store the objects in a Set in the first place so that conversion is entirely unnecessary.
Set
One also has to keep in mind that V8 has a spread optimization which is going to penalize overuse of the spread syntax on other JavaScript engines.
Perflink
withFilter
withLoopSet
withLoopIndexOf
withLoop
withSet
withFor
control
Results for Chromium 96 on Ubuntu 20.04
The shape of that chart changes wildly with each run. To some degree that is to be expected as JavaScript performance is notoriously unpredictable.
Judging from the results on Firefox (94.0)
combining filter with Set doesn't help that much away from V8.
filter
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Your sample code moves the temporary objects to the top level which exaggerates the difference.
There is no reason to store the objects in a
Set
in the first place so that conversion is entirely unnecessary.One also has to keep in mind that V8 has a spread optimization which is going to penalize overuse of the spread syntax on other JavaScript engines.
Perflink
withFilter
withLoopSet
withLoopIndexOf
withLoop
withSet
withFor
control
(withFor
)Results for Chromium 96 on Ubuntu 20.04
The shape of that chart changes wildly with each run. To some degree that is to be expected as JavaScript performance is notoriously unpredictable.
Judging from the results on Firefox (94.0)
combining
filter
withSet
doesn't help that much away from V8.