DEV Community

Discussion on: Recreating Lodash: _.pull()

Collapse
 
miketalbot profile image
Mike Talbot ⭐

Hey I like the idea of this article, but I do have a couple of comments:

  • The execution time of your function is heavy if values is large, basically making the execution time O(array.length * values.length). An O(array.length) implementation might look like:
function pull(array, values) {
    const inclusion = new Set(values)
    return array.filter(value=>!inclusion.has(value))
}
  • I find it odd to declare a top level function as a "fat-arrow". It's building something to capture global this and that's wasteful. It requires that you declare functions in sequence, which can become a pain and it's just not clear to me that a const is a function - plus who knows how this gets highlighted - but code pen didn't work out it was a function.

  • Making new arrays all the time has a significant impact on memory and execution time through garbage collection. I get immutability is an important style for reducing complexity, but sometimes it worries me to hell when a developer is taking a temporary array from the previous step and making yet another one.