What is difference between new Array(...set) and [...set]?

Viewed 117

I use next code for 500k+ array elements and gets max call stack error:

function getSortedUniqInts(arr) {
  const unique = new Set(arr);
  return new Array(...unique).sort((a, b) => a - b);
}

But this code works without the error:

function getSortedUniqInts(arr) {
  const unique = new Set(arr);
  return [...unique].sort((a, b) => a - b);
}

What is the difference between these?

0 Answers
Related