Last active
May 26, 2023 04:39
-
-
Save dherges/86012049be7b1263b2e594134ff5816a to your computer and use it in GitHub Desktop.
Simple LRU Cache in TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
@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.
@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
here my (slightly) adapted version,
following best practices in frontend/js/ts
of "Stop Using JavaScript Classes!" - https://dev.to/giantmachines/stop-using-javascript-classes-33ij