Open
Description
What problem are you trying to solve?
new TextEncoder().encode(input).byteLength
is an order of magnitude slower than alternatives, including Node's Buffer.byteLength(input)
and even handwritten JavaScript implementations.
./benchmarks/blob.js: 202’345.0 ops/sec (± 13’993.9, p=0.001, o=0/100)
./benchmarks/buffer.js: 57’434’701.2 ops/sec (±425’763.3, p=0.001, o=9/100) severe outliers=5
./benchmarks/implementation.js: 48’441’909.6 ops/sec (±397’249.6, p=0.001, o=5/100) severe outliers=2
./benchmarks/textencoder.js: 2’667’052.4 ops/sec (±564’727.5, p=0.001, o=6/100) severe outliers=2
My benchmark repo includes a JS implementation that I believe is at least close enough to correct for benchmarking purposes, although I'm no expert in UTF-16 so there may be some mistakes.
What solutions exist today?
new Blob([input]).size
new TextEncoder(input).byteLength
Buffer.byteLength(input)
(Node only)- Implementations in JS
How would you solve it?
let encoder = new TextEncoder()
let byteLength = encoder.byteLength("Hello, World!")
// >> 13
Anything else?
No response