typescript / intermediate
Snippet
Fast Buffer Operations Using Fixed-Length Typed Arrays
Typed arrays store primitive numbers in contiguous memory allocations, eliminating garbage collection pauses and dynamic array resizing overhead during intensive data processing.
snippet.ts
typescript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Int32RingBuffer {private buffer: Int32Array;private head: number = 0;constructor(capacity: number) {this.buffer = new Int32Array(capacity);}public push(value: number): void {this.buffer[this.head % this.buffer.length] = value;this.head++;}public getRawData(): Readonly<Int32Array> {return this.buffer;}}
Breakdown
1
private buffer: Int32Array;
Uses a high-performance, fixed-size contiguous memory block for 32-bit signed integers.
2
this.buffer = new Int32Array(capacity);
Allocates raw array buffer space up-front to eliminate dynamic reallocation costs.
3
this.buffer[this.head % this.buffer.length] = value;
Implements circular index calculation for fast O(1) overwrite semantics.
4
public getRawData(): Readonly<Int32Array> {
Exposes read-only views over typed array buffers without copying internal memory.