Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array.forEach.call vs array.map.call

To loop over the results of querySelectorAll in JavaScript, is one of the following more preferable than the other?

[].forEach.call(document.querySelectorAll('div'){
  // do something
})

[].map.call(document.querySelectorAll('div'){
  // do something
})

Essentially, I'm wondering if these each achieve the same result of providing access to each dom element returned from querySelectorAll. If so, what are reasons one might want to use one over the other?

like image 665
Isaac Gregson Avatar asked Nov 08 '14 09:11

Isaac Gregson


People also ask

Which is better forEach or map?

The main difference between map and forEach is that the map method returns a new array by applying the callback function on each element of an array, while the forEach method doesn't return anything. You can use the forEach method to mutate the source array, but this isn't really the way it's meant to be used.

What is the difference between the map () and the forEach () methods?

The map() method returns a new array, whereas the forEach() method does not return a new array. The map() method is used to transform the elements of an array, whereas the forEach() method is used to loop through the elements of an array.

What is the difference between map and forEach method on the array prototype?

The first difference between map() and forEach() is the returning value. The forEach() method returns undefined and map() returns a new array with the transformed elements. Even if they do the same job, the returning value remains different.

Is map slower than forEach?

forEach() is still slower, but not by as much as . map() (550-700ms). My guess is that . map() performs some additional logic that slows it down significantly compared to a raw for loop.


2 Answers

forEach operates on the original array elements. (If you want just iterate over all element you should use forEach)

map is running through your array, applying a function to each element, and emitting the result as a new array. (if you want to apply some changes to each element you should use map)

like image 149
Oleksandr T. Avatar answered Sep 28 '22 01:09

Oleksandr T.


There is a subtle difference in which elements will be looped over between map and forEach. If the element in the array is undefined, it will not be invoked in map, but it will be invoked on forEach.

Obviously this distinction does not apply in the case of querySelectorAll, which will never return undefined in its results.

So the only difference between them is that of what the function returns. forEach has no return value. map executes a function on each member of the array and returns the results. So, for instance, you could do this:

var values = [].map.call(document.querySelectorAll('input'), function(el) {
    return el.value;
});

This will return an array containing the value of every element selected by querySelectorAll('input').

So you should use map when that's what you want; otherwise, use forEach.

NB that Array.prototype.every and Array.prototype.some also exist; there may be times when they would be more appropriate.

like image 30
lonesomeday Avatar answered Sep 28 '22 02:09

lonesomeday