A pricing service, a feature-flag store or a collaborative document often has to answer "what was this value at 14:05?", to explain a past order, replay a bug report, or show an audit trail. A plain Map only knows the present. A map that keeps a time-stamped list of every change per key can answer for any moment.
Write the class HistoryMap in history-map.js. It works like a Map for the present, and also records every set and delete with the time Date.now() returns, so getAt(key, time) can return the value a key held at any past time.
classHistoryMap {getsize() // how many keys are present nowset(key, value) // returns the mapdelete(key) // true if the key was present, otherwise falsehas(key) // true or false, nowget(key) // the present value, or undefinedgetAt(key, time) // the value at that time, or undefinedhistory(key) // [{ time, value, deleted }, ...], oldest first}
time is in the same milliseconds as Date.now().
In history, a set is { time, value, deleted: false } and a delete is { time, value: undefined, deleted: true }.
get, has and size describe the present, as in a Map; set returns the map. A key set to undefined is present.
Keys can be any value: 1 and "1" are different keys, and objects and NaN work.
getAt(key, time) returns the value of the latest change made at or before time: the change made exactly at time counts. Before a key's first change it returns undefined.
delete(key) removes the key from the present and returns true; the past is kept, so getAt at an earlier time still returns the old value, and from the delete on it returns undefined.
A delete of a key that is not present records nothing and returns false. A key can be set again after a delete.
When several changes have the same time, the last one wins.
Every set is recorded, even one that keeps the same value.
history(key) returns every change of the key, oldest first, as new objects in a new array (changing them does not change the map); an unknown key gives [].
Values are stored as they are, not copied.
If Date.now() returns a time earlier than the key's last change (the system clock moved back), the change is recorded at the time of that last change, so the history stays in order.
getAt stays correct for a history of tens of thousands of changes; it should not have to scan the whole history.
// Date.now() is 1000 hereconstprices=newHistoryMap();prices.set("apple", 10); // at 1000// ... 500 ms laterprices.set("apple", 12); // at 1500prices.get("apple"); // 12prices.getAt("apple", 1200); // 10prices.getAt("apple", 1500); // 12: the change at exactly 1500 countsprices.getAt("apple", 999); // undefined: before the first change
JAVASCRIPT
constflags=newHistoryMap();flags.set("beta", true); // at 2000flags.delete("beta"); // at 2300: trueflags.has("beta"); // falseflags.getAt("beta", 2100); // true: the past is keptflags.history("beta");// [{ time: 2000, value: true, deleted: false },// { time: 2300, value: undefined, deleted: true }]
JAVASCRIPT
constmap=newHistoryMap();map.set("a", 1);map.set("a", 2); // in the same millisecondmap.getAt("a", Date.now()); // 2: the last change wins