Reputation: 11855
Assume a server-side JavaScript environment, that provides a function like so:
var parseIsoDuration = /... complex regex .../
function dateDiff(date, isoDurationStr){
var duration = parseIsoDuration.exec(isoDurationStr);
return timeLibrary.add(date, duration);
}
That function will be called from outside, on hundreds of dates, but mostly the same ISO duration. Because RexExp parsing is not a cheap operation, an application-level cache could be implemented:
var parseIsoDuration = /... complex regex .../
var durationCache = {}
function dateDiff(date, isoDurationStr){
var duration;
if (durationCache[isoDurationStr] === undefined){
duration = parseIsoDuration.exec(isoDurationStr);
durationCache[isoDurationStr] = duration;
} else {
duration = durationCache[isoDurationStr];
}
return timeLibrary.add(date, duration);
}
The problem: the server may run for a year straight, and the cache object never goes out of scope. If the function is called with a lot different ISO duration strings, the cache will grow over time and never reduce its size.
Is there a way to tell V8 that it may clear "old" entries from the cache object as needed (i.e. has grown too large)? Or at least make it clear it entirely as the need arises? (Solution may depend on ES6/7 features)
ES6 WeakMaps sound interesting, but they accept objects as keys only. Wrapping a string in an array to make it an object will not work, because doing that again will result in a different object. I would need something like Symbol(str)
, but that returns an identical reference for two identical inputs (Ref(str) === Ref(str)
).
edit: There's actually Symbol.for("str") === Symbol.for("str")
, but it's not accepted as WeakMap key.
I'm also not sure when entries would actually be garbage-collected in case of a WeakMap - it might be more or less immediately, because there would be no reference to the object right after it was added to the WeakMap. So a double no.
Unsetting individual keys would require additional book-keeping when they were added and some algorithm to determine a sensible TTL.
Is it necessary to cache the RegExp results even? Is there some built-in cache? For literal expressions or if created via constructor only, or both?
Upvotes: 0
Views: 458
Reputation: 13570
What you are talking about sounds like Java's SoftReference and no, there is nothing like this in v8. Instead you should manage cache yourself or use one of the modules like lru-cache
Upvotes: 1