Home > front end >  Best shallow copy of array?
Best shallow copy of array?

Time:09-23

When I was reading the vue source code today, I encountered a writing method that I didn't understand.
view source

const deduped = [...new Set(pendingPostFlushCbs)]

In my opinion, it is a shallow copy of the array. But why should I convert to Set first,can I have better performance? How should I understand it?

CodePudding user response:

With Set you remove duplicates:

pendingPostFlushCbs = [1,2,5,5,6,8,8,]
const deduped = [...new Set(pendingPostFlushCbs)]
const deduped1 = [...pendingPostFlushCbs]
console.log(deduped)
console.log(deduped1)

  • Related