Best loop method
array.forEach(item => {
sum += item
});
array.reduce((sum, num) => sum + num, 0);
for(const num of arr) {
sum += num;
}
for(let i=0; i<arr.length; i++) {
sum += arr[i];
}
Traditional for loop is the fastest way to go through array of elements and do any calculations. Of course, with number of elements about few hundreds, performance is almost even. Advantage is seen, when works on great number of elements (tausends).
But it is good to keep healthy habits.
Searching on array with non number elements (text, emails etc).
// standard includes method
for(const value of lookupValues) {
testData.includes(value);
}
// using Set of Data
const testSet = new Set(testData);
for(const value of lookupValues) {
testSet.has(value);
}
Using new Set with searching with has, is million times faster than includes. Why? Because Set makes elements indexed. So searching is much faster.
Worth only for huge arrays (on small, preparing new Set, can consume advantage).
Sorting elements
bubbleSort(array);
quickSort(array);
mergeSort(array);
array.toSorted();
The fastest one is quickSort(). Why is that? Because it is the simplest sorting algorithm. Fancier algorithms have tendency to be harder to implement and be more buggier.
So, keep it simple.