jonschlinkert / arr-diff

Returns an array with only the unique values from all given arrays using strict equality for comparisons.
https://github.com/jonschlinkert
MIT License
46 stars 18 forks source link

Smaller/more performant? #14

Open jimmywarting opened 3 years ago

jimmywarting commented 3 years ago

I was thinking maybe it could be possible to use Set instead to do the same thing... A benchmark for it maybe?

var a = ['a', 'b', 'c', 'd'];
var b = ['b', 'c'];

var set = new Set(a)
b.forEach(a => set.delete(a))
Array.from(set) // => ['a', 'd']

Also in many situations it's wasteful to cast the set into an array... ppl only do it to keep the same signature and never used set in the first place

curious to see a benchmark for this native solution

ChocolateLoverRaj commented 2 years ago

Also in many situations it's wasteful to cast the set into an array... ppl only do it to keep the same signature and never used set in the first place

@jimmywarting so do you want to add another function which you call like this:

anotherDiffFn(setToRemoveValuesFrom, arrayOfThingsToRemove)

?

jimmywarting commented 2 years ago

I was thinking more in terms of:

// index.js
export default function diff (a, b) {
  const set = new Set(a)
  b.forEach(a => set.delete(a))
  return set
}
import diff from 'arr-diff'

var a = ['a', 'b', 'c', 'd']
var b = ['b', 'c']

console.log(diff(a, b))
//=> Set(2) {'a', 'b'}

doe it would be breaking change...