1

I have a question about map.As you know by using map we can iterate arrays and able to change the element of arrays.To do that there are two ways and I listed them below.Which way has less complexity and more performance?(In the examples arrays are not big but think about the real world like 500 elements in an array)

var numbers = [4, 9, 16, 25];
numbers=numbers.map(e=>{
 return e=e*2;
})

In this approach assigning each returned value to current array.

numbers.map((e,a)=>{
  numbers[a]=e*2;
})

In this one on each iteration we need to go to array by index to find element and I think this is worse than above but Im not sure.

Timuçin Çiçek
  • 1,516
  • 3
  • 14
  • 39
  • 5
    No need for a map if you do not need a new Array. Just use forEach – mplungjan May 30 '20 at 13:32
  • As I know,map has less complexity then forEach but difference is very very less – Timuçin Çiçek May 30 '20 at 13:33
  • 1
    No. map creates a new array and allows representing a chainable functional transformation. It generally represents directly transforming object shape more elegantly. The other way mutates the array in place. Performance is normally negligible, but mutating in place avoids object initalization, so will outperform if the amount of data to manipulate is very very large – user120242 May 30 '20 at 13:35
  • 1
    The "complexity" of `.map()` and `.forEach()` are basically the same, though because `.forEach()` does not create a new array it's slightly *less* "complex". – Pointy May 30 '20 at 13:39
  • second approach will be more efficient if `forEach` is used in place of `map` it will save space whereas no impact on time. – JackOfAshes - Mohit Gawande May 30 '20 at 13:45
  • Does this answer your question? [When/why to use map/reduce over for loops](https://stackoverflow.com/questions/29640254/when-why-to-use-map-reduce-over-for-loops) – user120242 May 30 '20 at 13:58

4 Answers4

5

Don't use map if you're not going to use the array it creates. If you just want to loop through an array, use a loop or forEach or similar.

Whether you want a new array or not, the most efficient way to loop through an array is usually a for loop:

const numbers = [4, 9, 16, 25];
for (let i = 0, len = numbers.length; i < len; ++i) {
    numbers[i] = numbers[i] * 2;
}

(Or any of several variations on that.)

But "most efficient" is extremely unlikely to actually matter in real-world code, so using things like map (if you want a new array) or forEach (if you don't) is just fine. map is idiomatic for that operation (creating a new array containing the entries from a previous array modified in some way).

Conclusion:

If you want a new array, feel free to use map, or create a blank array and use any of your options for looping arrays to loop the original and add entries to the new one.

If you don't want a new array, don't use map, use any of your options for iterating over the original except for ones that don't give you the index, and assign the updated value back to the entry.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

Semantically you need for or forEach when you do not need a new array. For sufficiently small arrays there is no difference in performance

var numbers = [4, 9, 16, 25];
numbers.forEach((e,i)=> numbers[i] = e*2);
console.log(numbers)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1
nums.forEach((v, i) => nums[i] = v);

is the fastest method possible that you listed (equivalent to your second method. Why? nums = nums.map(...) creates an entire copy of the array in memory and replaces the array with that copy. When you simply iterate over the array with an index and replace elements, you avoid making that copy, and it ends up being around 15% faster (see this benchmark.

Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • 1
    [Which is faster](https://ericlippert.com/2012/12/17/performance-rant/) – Andreas May 30 '20 at 13:38
  • 1
    *"...is the fastest method possible..."* No, a `for` loop is, since it avoids all those function calls. It's just extremely unlikely to matter, because function calls are *really really fast*. :-) – T.J. Crowder May 30 '20 at 13:41
0

The fastest way to change a value of an array is to iterate the index and update the item.

This approach is faset than forEach or map, because it does not have the overhead of this and it does not need the feature of the supplied structure of the callback.

var numbers = [4, 9, 16, 25];

for (let i = 0; i < numbers.length; i++) numbers[i] *= 2;

console.log(numbers);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392