A robust way to implement a [[Simple Moving Average (SMA)]], for instance, is with the help of a ring buffer. Array operations involve dynamic memory allocations. They are a big performance penalty, which matters when you need to perform the same operation on a continuous basis. To avoid such allocations, we allocate memory on the stack instead. Since the size of the window is known at runtime, we can define a fixed size array. After that, it's a matter of: 1. Inserting the initial values 2. Replacing the values one after the other by keeping track of the replacement index By doing so, we treat the fixed size array as if it were a ring, where the latest value naturally replaces the oldest. Below is a JavaScript example implementation for moving averages. ```js class RingBuffer { constructor(size) { this.buffer = new Float32Array(size); this.size = size; this.index = 0; this._sum = 0; this.full = false; this._avg = 0; } push(next) { const prev = this.buffer[this.index]; this.buffer[this.index] = next; this.sum = this.sum - prev + next; this.index = (this.index + 1) % this.size; if (this.index === 0) this.full = true; this.avg = this.full ? this.sum / this.size : 0; return this.avg; } get avg() { return this._avg; } set avg(a) { this._avg = a; } get sum() { return this._sum; } set sum(s) { this._sum = s; } } ```