Each entry carries a visited bit that get sets. A hand walks from the
oldest entry toward the newest, clearing set bits, and evicts the first
entry whose bit is clear; it stays where it stopped for the next eviction.
An entry read since the hand last passed it therefore survives one more
pass, and new entries never move older ones.
Choose it as the default for skewed traffic, where a minority of keys takes
most reads. During a one-pass scan of keys never read again, the hand
clears each read entry once and then chases the scanned keys toward the
head, evicting them rather than the read entries behind it; but when read
entries fill all but one slot, the hand walks off the head, wraps to the
tail, and the scan pushes out every entry not read during it.
get and a set of a key already held count as uses; peek and has
do not.
get, peek, has, and delete run in O(1) average time. set runs
in O(1) amortized time per entry it evicts: the hand clears each visited
bit at most once per set. Iteration is O(n) over every slot allocated
since the last clear.
@throws ― {CacheInvalidInputError} With INVALID_CAPACITY when capacity is
not a positive safe integer (or, with weigh, not a positive finite
number), MISSING_CLOCK when timeToLive comes without clock, or
INVALID_TIME_TO_LIVE when timeToLive is not a positive finite number.
Each entry carries a visited bit that get sets. A hand walks from the
oldest entry toward the newest, clearing set bits, and evicts the first
entry whose bit is clear; it stays where it stopped for the next eviction.
An entry read since the hand last passed it therefore survives one more
pass, and new entries never move older ones.
Choose it as the default for skewed traffic, where a minority of keys takes
most reads. During a one-pass scan of keys never read again, the hand
clears each read entry once and then chases the scanned keys toward the
head, evicting them rather than the read entries behind it; but when read
entries fill all but one slot, the hand walks off the head, wraps to the
tail, and the scan pushes out every entry not read during it.
get and a set of a key already held count as uses; peek and has
do not.
get, peek, has, and delete run in O(1) average time. set runs
in O(1) amortized time per entry it evicts: the hand clears each visited
bit at most once per set. Iteration is O(n) over every slot allocated
since the last clear.
@throws ― {CacheInvalidInputError} With INVALID_CAPACITY when capacity is
not a positive safe integer (or, with weigh, not a positive finite
number), MISSING_CLOCK when timeToLive comes without clock, or
INVALID_TIME_TO_LIVE when timeToLive is not a positive finite number.
, it counts
entries and must be a positive safe integer. With
weigh
, it bounds
the exact sum of the supplied binary floating-point weights and may be
any positive finite number. There is no rounding tolerance at the bound.
Store the value under the key, replacing any value already there, after
evicting what the policy chooses until the entry fits. The key just set
is never evicted by its own call.
@returns ― The entries evicted to make room, in eviction order. Expired
entries removed along the way are not reported. Empty when nothing was
evicted.
@throws ― {CacheInvalidInputError} With code INVALID_VALUE when the value
is undefined, INVALID_WEIGHT when weigh returns anything but a
positive finite number, ENTRY_TOO_HEAVY when the entry alone outweighs
capacity, or INVALID_CLOCK_READING when the injected clock returns a
non-finite number. The cache is unchanged after any of these. A
customCache also throws INVALID_VICTIM when its policy's
chooseVictim names a key the cache does not hold or the key being set.
Store the value under the key, replacing any value already there, after
evicting what the policy chooses until the entry fits. The key just set
is never evicted by its own call.
@returns ― The entries evicted to make room, in eviction order. Expired
entries removed along the way are not reported. Empty when nothing was
evicted.
@throws ― {CacheInvalidInputError} With code INVALID_VALUE when the value
is undefined, INVALID_WEIGHT when weigh returns anything but a
positive finite number, ENTRY_TOO_HEAVY when the entry alone outweighs
capacity, or INVALID_CLOCK_READING when the injected clock returns a
non-finite number. The cache is unchanged after any of these. A
customCache also throws INVALID_VICTIM when its policy's
chooseVictim names a key the cache does not hold or the key being set.
set('b', 2) // []
constcache:Cache<string, number>
cache.
Cache<string, number>.get: (key:string) => number |undefined
Return the value stored under the key, counting the read as a use for the
eviction policy, or undefined when the key is absent or expired.
@throws ― {CacheInvalidInputError} With code INVALID_CLOCK_READING when
the injected clock returns a non-finite number.
Store the value under the key, replacing any value already there, after
evicting what the policy chooses until the entry fits. The key just set
is never evicted by its own call.
@returns ― The entries evicted to make room, in eviction order. Expired
entries removed along the way are not reported. Empty when nothing was
evicted.
@throws ― {CacheInvalidInputError} With code INVALID_VALUE when the value
is undefined, INVALID_WEIGHT when weigh returns anything but a
positive finite number, ENTRY_TOO_HEAVY when the entry alone outweighs
capacity, or INVALID_CLOCK_READING when the injected clock returns a
non-finite number. The cache is unchanged after any of these. A
customCache also throws INVALID_VICTIM when its policy's
chooseVictim names a key the cache does not hold or the key being set.
set('c', 3) // [{ key: 'b', value: 2 }], what was evicted to make room
constcache:Cache<string, number>
cache.
Cache<string, number>.peek: (key:string) => number |undefined
Return the value stored under the key without counting the read as a use,
or undefined when the key is absent or expired.
@throws ― {CacheInvalidInputError} With code INVALID_CLOCK_READING when
the injected clock returns a non-finite number.
peek('b') // undefined
constcache:Cache<string, number>
cache.
Cache<string, number>.size: number
Number of entries held, including expired entries nothing has touched yet. O(1).
Each entry carries a visited bit that get sets. A hand walks from the
oldest entry toward the newest, clearing set bits, and evicts the first
entry whose bit is clear; it stays where it stopped for the next eviction.
An entry read since the hand last passed it therefore survives one more
pass, and new entries never move older ones.
Choose it as the default for skewed traffic, where a minority of keys takes
most reads. During a one-pass scan of keys never read again, the hand
clears each read entry once and then chases the scanned keys toward the
head, evicting them rather than the read entries behind it; but when read
entries fill all but one slot, the hand walks off the head, wraps to the
tail, and the scan pushes out every entry not read during it.
get and a set of a key already held count as uses; peek and has
do not.
get, peek, has, and delete run in O(1) average time. set runs
in O(1) amortized time per entry it evicts: the hand clears each visited
bit at most once per set. Iteration is O(n) over every slot allocated
since the last clear.
@throws ― {CacheInvalidInputError} With INVALID_CAPACITY when capacity is
not a positive safe integer (or, with weigh, not a positive finite
number), MISSING_CLOCK when timeToLive comes without clock, or
INVALID_TIME_TO_LIVE when timeToLive is not a positive finite number.
Each entry carries a visited bit that get sets. A hand walks from the
oldest entry toward the newest, clearing set bits, and evicts the first
entry whose bit is clear; it stays where it stopped for the next eviction.
An entry read since the hand last passed it therefore survives one more
pass, and new entries never move older ones.
Choose it as the default for skewed traffic, where a minority of keys takes
most reads. During a one-pass scan of keys never read again, the hand
clears each read entry once and then chases the scanned keys toward the
head, evicting them rather than the read entries behind it; but when read
entries fill all but one slot, the hand walks off the head, wraps to the
tail, and the scan pushes out every entry not read during it.
get and a set of a key already held count as uses; peek and has
do not.
get, peek, has, and delete run in O(1) average time. set runs
in O(1) amortized time per entry it evicts: the hand clears each visited
bit at most once per set. Iteration is O(n) over every slot allocated
since the last clear.
@throws ― {CacheInvalidInputError} With INVALID_CAPACITY when capacity is
not a positive safe integer (or, with weigh, not a positive finite
number), MISSING_CLOCK when timeToLive comes without clock, or
INVALID_TIME_TO_LIVE when timeToLive is not a positive finite number.
, it counts
entries and must be a positive safe integer. With
weigh
, it bounds
the exact sum of the supplied binary floating-point weights and may be
any positive finite number. There is no rounding tolerance at the bound.
capacity: 1_000 })
let
let evicted:number
evicted=0
// 100 hot keys, read again between 20,000 inserts of keys seen once.
Store the value under the key, replacing any value already there, after
evicting what the policy chooses until the entry fits. The key just set
is never evicted by its own call.
@returns ― The entries evicted to make room, in eviction order. Expired
entries removed along the way are not reported. Empty when nothing was
evicted.
@throws ― {CacheInvalidInputError} With code INVALID_VALUE when the value
is undefined, INVALID_WEIGHT when weigh returns anything but a
positive finite number, ENTRY_TOO_HEAVY when the entry alone outweighs
capacity, or INVALID_CLOCK_READING when the injected clock returns a
non-finite number. The cache is unchanged after any of these. A
customCache also throws INVALID_VICTIM when its policy's
chooseVictim names a key the cache does not hold or the key being set.
Store the value under the key, replacing any value already there, after
evicting what the policy chooses until the entry fits. The key just set
is never evicted by its own call.
@returns ― The entries evicted to make room, in eviction order. Expired
entries removed along the way are not reported. Empty when nothing was
evicted.
@throws ― {CacheInvalidInputError} With code INVALID_VALUE when the value
is undefined, INVALID_WEIGHT when weigh returns anything but a
positive finite number, ENTRY_TOO_HEAVY when the entry alone outweighs
capacity, or INVALID_CLOCK_READING when the injected clock returns a
non-finite number. The cache is unchanged after any of these. A
customCache also throws INVALID_VICTIM when its policy's
chooseVictim names a key the cache does not hold or the key being set.
set(
let index:number
index, `value-${
let index:number
index}`).
ReadonlyArray<CacheEntry<number, string>>.length: number
Gets the length of the array. This is a number one higher than the highest element defined in an array.
length
}
constcache:Cache<number, string>
cache.
Cache<number, string>.size: number
Number of entries held, including expired entries nothing has touched yet. O(1).
size// 1000
let evicted:number
evicted// 19100, every insert after the first 1,000 evicted one entry
Determines whether all the members of an array satisfy the specified test.
@param ― predicate A function that accepts up to three arguments. The every method calls
the predicate function for each element in the array until the predicate returns a value
which is coercible to the Boolean value false, or until the end of the array.
@param ― thisArg An object to which the this keyword can refer in the predicate function.
If thisArg is omitted, undefined is used as the this value.
Return the value stored under the key without counting the read as a use,
or undefined when the key is absent or expired.
@throws ― {CacheInvalidInputError} With code INVALID_CLOCK_READING when
the injected clock returns a non-finite number.
peek(1_000) // undefined, a key read once is gone
The same options also bound total weight with weigh, and expire entries with timeToLive and a clock you pass in. The module page compares SIEVE, LRU, and S3-FIFO on a scan-heavy trace.