Yomguithereal / mnemonist

Curated collection of data structures for the JavaScript/TypeScript language.
https://yomguithereal.github.io/mnemonist
MIT License
2.26k stars 92 forks source link

[LRU] Remove duplicate code between set and setpop #160

Closed trivikr closed 3 years ago

trivikr commented 3 years ago

Is your request related to a problem? Please describe.

The functions set and setpop are very similar:

Describe the solution you'd like

Call setpop from inside set and ignore the returned value.

For example, in LRUCache:

LRUCache.prototype.set = function(key, value) {
  this.setpop(key, value);
}

Describe alternatives you've considered

N/A

Additional context

Yomguithereal commented 3 years ago

Hello again @trivikr. Once again, this redundant code is here for a reason. The #.setpop method needs to allocate an object to work and will trigger garbage collection when used repeatedly, something that the #.set method should avoid. Modern JS engines may be able to optimize for this in your example since the return value is not used by I doubt it very much :)

trivikr commented 3 years ago

this redundant code is here for a reason. The #.setpop method needs to allocate an object to work and will trigger garbage collection when used repeatedly, something that the #.set method should avoid.

This issue can be closed as the duplicate code exists for performance reasons.