Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster toString('hex') implementation #364

Open
jawj opened this issue Nov 12, 2024 · 5 comments
Open

Faster toString('hex') implementation #364

jawj opened this issue Nov 12, 2024 · 5 comments

Comments

@jawj
Copy link

jawj commented Nov 12, 2024

In a similar vein to #245, I have an implementation of toString('hex') that's between 2x and 4x faster than the existing one, depending on the browser/engine.

See: https://jsbench.me/evm3ejel2i/3

Possible drawbacks of this implementation:

  • Requires TextDecoder (does this package support IE currently?)
  • Likely uses more memory, since it allocates a Uint16Array to hold intermediate data

Would you accept a PR to switch to this implementation?

@jawj
Copy link
Author

jawj commented Nov 12, 2024

Thinking about it, we can probably limit the extra memory needed by doing it in chunks ...

@jawj
Copy link
Author

jawj commented Nov 13, 2024

I've packaged this code up on npm: https://www.npmjs.com/package/hextreme. For multi-megabyte arrays it can be many times faster than the approach currently taken here.

Are you willing to consider a PR to use this package for .toString('hex')?

@dcousens
Copy link
Collaborator

dcousens commented Nov 13, 2024

I think we reasonably track Node LTS for the Buffer API, and with IE officially out of support - I'm happy to say we that we could drop support for that in respect of code used.

@jawj
Copy link
Author

jawj commented Nov 14, 2024

Thanks. I actually added a fallback implementation where there's no TextDecoder anyway, which is still slightly faster than the current approach because it does a bit of loop unrolling.

I also limited the biggest possible temporary buffer allocation to 1MB, by doing the conversion in chunks, which makes very little difference in speed.

I'll go ahead and submit a PR in the next few days then.

@jawj
Copy link
Author

jawj commented Nov 14, 2024

I've actually just applied the same technique to base64 encoding, so I might submit another PR for that at some point.

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

No branches or pull requests

2 participants