0

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?

1
  • 1
    No, this is not possible with JS today, it would need truly weak references. I don't know whether there exist any node packages that provide them through low-level APIs. Commented Sep 4, 2015 at 12:55

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

The LRU cache module looks like a very good solution. Could you add something about V8 regexp caching behavior though?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.