Skip to content

Instantly share code, notes, and snippets.

@bitifet
Created January 14, 2024 22:15
Show Gist options
  • Save bitifet/6d64eeb8a4d0e19f726347c6d6f6e702 to your computer and use it in GitHub Desktop.
Save bitifet/6d64eeb8a4d0e19f726347c6d6f6e702 to your computer and use it in GitHub Desktop.
Just a simple Array Cache that I finally won't use by now... πŸ˜ƒ
export class smallCache extends Array {
constructor(maxlength = 10) {
super();
Object.defineProperty(this, "maxlength", {
value: maxlength,
enumerable: false, });
};
push(...args) {
super.push(...args);
const excess = Math.max(0, this.length - this.maxlength);
for (let i=excess; i>0; i--) this.shift();
return this.length;
};
unshift(...args) {
super.unshift(...args);
if (this.length > this.maxlength) this.length = this.maxlength;
return this.length;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment