in general a while loop is the fastest (but sometimes not, 98% of the time it will). its not recommended. Because filter is also using a while loop but also checks the values if undefined etc. It is safer to use filter. Or we need to check manually. Which would lead in a slower approach.
yes.
some more functions: LINK
in general a while loop is the fastest (but sometimes not, 98% of the time it will). its not recommended. Because filter is also using a while loop but also checks the values if undefined etc. It is safer to use filter. Or we need to check manually. Which would lead in a slower approach.
The whole Set loop method it also fast, but when you need to return an array instead its slows the function down.
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
Setin 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
withFilterwithLoopSetwithLoopIndexOfwithLoopwithSetwithForcontrol(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
filterwithSetdoesn't help that much away from V8.