Skip to content

Instantly share code, notes, and snippets.

@dherges
Last active May 26, 2023 04:39
Show Gist options
  • Save dherges/86012049be7b1263b2e594134ff5816a to your computer and use it in GitHub Desktop.
Save dherges/86012049be7b1263b2e594134ff5816a to your computer and use it in GitHub Desktop.
Simple LRU Cache in TypeScript
class LruCache<T> {
private values: Map<string, T> = new Map<string, T>();
private maxEntries: number = 20;
public get(key: string): T {
const hasKey = this.values.has(key);
let entry: T;
if (hasKey) {
// peek the entry, re-insert for LRU strategy
entry = this.values.get(key);
this.values.delete(key);
this.values.set(key, entry);
}
return entry;
}
public put(key: string, value: T) {
if (this.values.size >= this.maxEntries) {
// least-recently used cache eviction strategy
const keyToDelete = this.values.keys().next().value;
this.values.delete(keyToDelete);
}
this.values.set(key, value);
}
}
@sagarpanchal
Copy link

@adaptive-shield-matrix
The article is a rant that proves nothing.
TS and JS both have private class properties.
OOP is way better for entity driven approach.

@adaptive-shield-matrix
Copy link

@sagarpanchal
could you expand more?
I don't know any good cases, where OOP is better.
And I haven't met an entity driven approach in any of the popular frontend frameworks (react, nextjs).
Are you maybe talking about gamedev and not frontend?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment