Node util:UTF8解码速度提高 274%

22-11-12 banq

修复 nodejs/performance#3
基准 CI:https ://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1219/

测试结果:

util/text-decoder.js type='ArrayBuffer' n=100 len=16384 ignoreBOM=0 encoding='iso-8859-3'                       -6.27 %       ±8.96% ±11.92% ±15.52%
util/text-decoder.js type='ArrayBuffer' n=100 len=16384 ignoreBOM=0 encoding='latin1'                           -4.62 %       ±8.01% ±10.67% ±13.90%
util/text-decoder.js type='ArrayBuffer' n=100 len=16384 ignoreBOM=0 encoding='utf-8'                    ***     85.34 %



与 Deno 和 Bun 进行比较的微基准(基准可在https://github.com/anonrig/node-benchmarks获得)

> node-benchmarks@1.0.0 text-decoder
> ~/Developer/node/out/Release/node ./text-decoder/index.mjs && bun run ./text-decoder/index.mjs && deno run -A ./text-decoder/deno.js

cpu: Apple M1 Max
runtime: node v20.0.0-pre (arm64-darwin)

benchmark       time (avg)             (min … max)       p75       p99      p995
-------------------------------------------------- -----------------------------
smallUint8  308.11 ns/iter  (298.87 ns … 331.5 ns) 310.62 ns 331.39 ns  331.5 ns
bigUint8      2.64 µs/iter      (2.6 µs … 2.73 µs)   2.64 µs   2.73 µs   2.73 µs
cpu: Apple M1 Max
runtime: bun 0.2.2 (arm64-darwin)

benchmark       time (avg)             (min … max)       p75       p99      p995
-------------------------------------------------- -----------------------------
smallUint8  213.82 ns/iter (200.47 ns … 461.84 ns) 216.17 ns 227.67 ns 243.92 ns
bigUint8      1.19 µs/iter     (1.07 µs … 1.36 µs)   1.23 µs   1.36 µs   1.36 µs
cpu: Apple M1 Max
runtime: deno 1.27.1 (aarch64-apple-darwin)

benchmark       time (avg)             (min … max)       p75       p99      p995
-------------------------------------------------- -----------------------------
smallUint8  458.98 ns/iter  (439.37 ns … 477.2 ns)    463 ns  476.7 ns  477.2 ns
bigUint8      3.81 µs/iter     (3.74 µs … 3.86 µs)   3.82 µs   3.86 µs   3.86 µs



使用方式:

// Buffer.from(string, 'utf8') is faster, when available
const toUtf8 = typeof Buffer === 'undefined'
  ? (value: string) => new TextEncoder().encode(value)
  : (value: string) => Buffer.from(value, 'utf8')



更多点击标题