CKTIS / JavaScript
Collections: stack, queue and LRU algorithms
A stack processes elements in LIFO order, a queue uses FIFO, and an LRU cache evicts the least recently used entry. JavaScript implementations can use Array and Map, but must deliberately handle ordering, operation costs and missing values.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
stack.push(value); stack.pop(); queue.push(value); queue.shift(); map.delete(key); map.set(key, value); map.keys().next().valueStack, queue and LRU algorithms define collection-management rules, not built-in constructor names. A stack uses LIFO: the last element added is removed first. An array with push() and pop() suffices for a simple stack. A queue uses FIFO: the first element added is removed first; a simple version uses push() and shift().
shift() may require moving indexed properties, so do not assume constant-cost removal from the front. For a large queue, consider a circular buffer or a head index with periodic compaction; release references to consumed objects. pop() and shift() return undefined for an empty array, which does not distinguish an empty collection from a stored undefined value.
LRU (least recently used) evicts the least recently used entry when capacity is exceeded. Map preserves insertion order, but neither get() nor set() on an existing key refreshes its position. Delete and reinsert the entry on a read hit and on a write; the first key from keys() then identifies the eviction candidate. The example has a fixed capacity of 2; a parameterized version should validate that capacity is a positive integer.
In the example, has() distinguishes a missing key from an entry storing undefined, although the public get() returns undefined in both cases. The specification requires average sublinear access for Map, not guaranteed O(1). The code uses syntax and facilities available in ECMAScript 2015; it requires no browser Web APIs. Running it in Node does not prove compatibility with a target browser.
Examples
const stack = [];
stack.push('a', 'b');
console.log(stack.pop());
const queue = [];
queue.push('a', 'b');
console.log(queue.shift());
const cache = new Map();
const capacity = 2;
function put(key, value) {
cache.delete(key);
cache.set(key, value);
if (cache.size > capacity) {
cache.delete(cache.keys().next().value);
}
}
function get(key) {
if (!cache.has(key)) return undefined;
const value = cache.get(key);
cache.delete(key);
cache.set(key, value);
return value;
}
put('a', 1);
put('b', 2);
console.log(get('a'));
put('c', 3);
console.log(cache.has('b'));
console.log([...cache.keys()].join(','));Result
b a 1 false a,c
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.