gleam-lang / stdlib

🎁 Gleam's standard library
https://hexdocs.pm/gleam_stdlib/
Apache License 2.0
492 stars 175 forks source link

Base64 encoding optimisations. Fix encoding of large bit arrays on JS. #643

Closed richard-viney closed 5 months ago

richard-viney commented 5 months ago

Fixes #642.

This replaces the implementation of Base64 encoding with a native JS implementation that is significantly faster, 13-14x in my benchmarks. It's based on https://github.com/mitschabaude/fast-base64/blob/main/js.js.

The new implementation works on large inputs, this was tested up to 128MiB on Node.js 22 with no issues.

The Erlang implementation also now gets base64:encode to add the padding to the returned string if it's requested, which optimises away a string.replace when padding isn't desired.