4 ms·
Won’t JS garbage collect orphaned references? Why is this necessary?
by withinboredom 4y ago
Won’t JS garbage collect orphaned references? Why is this necessary?
- stickupkid 4y agoIt does, but the map has a reference to it, so it will "leak" (in gc languages an unwanted reference is considered a leak). If this map got rather large, you could end up with a rather large heap and it would be un-obvious why at first.
- withinboredom 4y agoDo Promises hold a reference to the chain of functions that end in the result? If so, that seems like a bug.
- epolanski 4y agoA promise is just an object, it does not contain any reference to the chained functions.
- deckard1 4y agothere is some confusion here. OP is intentionally caching results of, presumably, expensive computations or network calls. It's a cache. There is no memory leak. OP just did not detail how cache invalidation/replacement happens. The 2nd comment adds a rather rudimentary mechanism of removing items from cache as soon as they are ready. You get the benefit of batching requests that occur before the resolve, but you get the downside of requests coming in right after the resolve hitting the expensive process again. Requests don't neatly line up at the door and stop coming in as soon as your database query returns. Typically you would use an LRU mechanism. All caches (memcached, redis, etc.) have a memory limit (whether fixed or unbounded, i.e. all RAM) and a cache replacement policy.