If we talk about efficiency, then we first need to figure out .map or .from:
map: maps over elements returning new array 👎
from: mapFn maps over elements in place 👍
So from, which leaves us with the question what we're going to create our array from, right? So if we compare:
// Array from arrayArray.from(Array(3),()=>Array(3).fill(0));// Array from objectArray.from({length:3},()=>Array(3).fill(0));
...it will boil down to the question what's more efficient to create Array(3) or { length: 3 }, and given arrays in JS are just objects it really comes to the number of properties we need to create for the object. How many properties does Array(3) have? (hint: 4) How many properties does { length: 3 } have? (hint: 1)
If we talk about efficiency, then we first need to figure out
.map
or.from
:map
: maps over elements returning new array 👎from
: mapFn maps over elements in place 👍So
from
, which leaves us with the question what we're going to create our array from, right? So if we compare:...it will boil down to the question what's more efficient to create
Array(3)
or{ length: 3 }
, and given arrays in JS are just objects it really comes to the number of properties we need to create for the object. How many properties doesArray(3)
have? (hint: 4) How many properties does{ length: 3 }
have? (hint: 1)I hope this will clear things a bit
Awesome and detailed explanation @snigo . ✌🏻
I have created GitHub Repo, feel free to contribute to that.
Thanks!🤗