buffer
buffer copied to clipboard
Update `hexSlice` to use TypedArrays and TextDecoder for large speed gains
trafficstars
Following up on #364 and #366, this patch switches the hexSlice implementation to use TypedArrays and TextDecoder. It also adds a performance benchmark for toString('hex').
In Node on an M3 Pro MacBook, the benchmark shows a 5x speed-up on a 4096-byte buffer, achieving 75% of the speed of the native method. The speed-up is generally larger as the buffer size increases.
Before:
BrowserBuffer#toString("hex") x 72,483 ops/sec ±0.19% (99 runs sampled)
NodeBuffer#toString("hex") x 527,266 ops/sec ±0.33% (97 runs sampled)
Fastest is NodeBuffer#toString("hex")
After:
BrowserBuffer#toString("hex") x 403,058 ops/sec ±0.30% (98 runs sampled)
NodeBuffer#toString("hex") x 533,325 ops/sec ±0.21% (98 runs sampled)
Fastest is NodeBuffer#toString("hex")