DEV Community

Discussion on: 8 JavaScript Tips & Tricks That No One Teaches 🚀

Collapse
 
dannyengelman profile image
Info Comment hidden by post author - thread only visible in this permalink
Danny Engelman

Interesting to learn that:

let dogsNames = Array.from(dogs, ({name}) => name);

is not as complicated as:

let dogsNames = dogs.map( ({name}) => name );

BUT..
the documentation says the y parameter in Array.from(x,y,z) IS a mapFn
So yes, you do not type those 3 letters... but you still have to understand you are mapping an Array.

Since you mention the Performance API; this gives a very interesting result:

let dogs = [
    { name: "Rio" , age: 2 },
    { name: "Mac", age: 3 },
    { name: "Bruno", age: 5 },
    { name: "Jucas", age: 10 },
    { name: "Furr", age: 8 },
    { name: "Blu", age: 7 },
];

function performance(method) {
  const firstTime = performance.now();
  let dogsNames;
  for (let i = 0; i < 10000; i++) {
    if (method == 1) {
      dogsNames = Array.from(dogs, ({name}) => name);
    } else {
      dogsNames = dogs.map(({name}) => name);
    }
  }
  const secondTime = performance.now();
  console.log(`The something function took ${secondTime - firstTime} milliseconds.`);
}
performance(1);
performance(2);
Enter fullscreen mode Exit fullscreen mode

Some comments have been hidden by the post's author - find out more