I have a dynamic array that can often be empty, and I need to iterate over all its elements.
So far I have such code:
array.forEach(function(item, index) {
//stuff here
});
It works fine of course, but I wonder if there is any overhead for the forEach method when the array is empty, in such case maybe better checking first, e.g.
if (array.length > 0) {
array.forEach(function(item, index) {
//stuff here
});
}
Which is better practice?