Created
January 14, 2024 22:15
-
-
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... π
This file contains hidden or 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
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