Skip to content

Instantly share code, notes, and snippets.

@remixer-dec
Created May 8, 2020 13:32
Show Gist options
  • Save remixer-dec/33fa16c0f4caa39eb460c4ee113ce3c6 to your computer and use it in GitHub Desktop.
Save remixer-dec/33fa16c0f4caa39eb460c4ee113ce3c6 to your computer and use it in GitHub Desktop.
Fixed size / length array (LiFo) for infinite pushing
//Warning: fixed size is only kept for .push() method
//Usage:
//let arr = new FixedSizeArrayLifo(2)
//arr.push(123); ...
class FixedSizeArrayLiFo extends Array {
constructor(size) {
super()
this.#size = size
}
#size
push(value) {
super.push(value)
if (this.length > this.#size) {
this.shift()
}
return this.length
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment