Skip to content

perf(convert): improve performance by prioritizing typed arrays - #71

Merged
polygonplanet merged 2 commits into
masterfrom
feature/perf-typed-array
Oct 12, 2026
Merged

polygonplanet merged 2 commits into
masterfrom
feature/perf-typed-array

Conversation

@polygonplanet

Copy link
Copy Markdown
Owner

Summary

Improved conversion performance by prioritizing typed arrays.

This makes convert() about 1.5-3.5x faster for most encoding pairs (about 1.3x when converting from UTF-8) and reduces peak memory during conversion by about 25-60%.

A plain Array is still used when typed arrays are not available, or when the output length cannot be determined in advance (e.g., with the { fallback: "html-entity" } option).

Fixes

  • Converting from UTF-8: A continuation byte (0x80-0xBF) without a lead byte is now ignored.
  • Converting to UTF-8: Code points above U+10FFFF are now replaced with the fallback character.
  • UTF-16: Input with an odd number of bytes no longer produces a trailing undefined element.

Benchmark

Node.js v24 with the full text of "I Am a Cat" (about 350,000 characters), type: 'array' (ops/sec, higher is better):

Conversion Before (v2.4.0) After Speedup
SJIS -> UNICODE 64.51 ops/sec 160 ops/sec 2.5x
EUCJP -> UNICODE 65.96 ops/sec 182 ops/sec 2.8x
JIS -> UNICODE 66.85 ops/sec 189 ops/sec 2.8x
UNICODE -> SJIS 23.63 ops/sec 54.25 ops/sec 2.3x
UNICODE -> EUCJP 15.56 ops/sec 39.48 ops/sec 2.5x
UNICODE -> JIS 26.98 ops/sec 45.01 ops/sec 1.7x
SJIS -> UTF8 84.82 ops/sec 154 ops/sec 1.8x
UTF8 -> SJIS 62.02 ops/sec 79.35 ops/sec 1.3x
UNICODE -> UTF8 54.85 ops/sec 183 ops/sec 3.3x
UTF8 -> UNICODE 266 ops/sec 343 ops/sec 1.3x

Peak memory (maxRSS) while repeating the same conversion 20 times (lower is better):

Conversion Before (v2.4.0) After
UTF8 -> SJIS 269 MB 201 MB
SJIS -> UTF8 226 MB 114 MB
UNICODE -> EUCJP 294 MB 170 MB
SJIS -> UNICODE 192 MB 77 MB

… bytes

- Ignore a UTF-8 continuation byte without a lead byte
- Convert values above U+10FFFF to the fallback character when converting to UTF-8
@polygonplanet
polygonplanet merged commit 21919c3 into master Oct 12, 2026
18 checks passed
@polygonplanet
polygonplanet deleted the feature/perf-typed-array branch October 12, 2026 14:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant