PitchHut
Log in / Sign up
hextreme
22 views
Fast hex string generation from Uint8Arrays and arrays of numbers.
Pitch

Hextreme offers a high-speed solution for converting Uint8Arrays and regular arrays of numbers to hex strings. With performance benchmarks that outpace native methods in many scenarios, this tool efficiently handles data conversion with smart memory management, ensuring rapid processing.

Description

hextreme is a high-performance utility designed to convert Uint8Array objects into hex strings swiftly. Inspired by the built-in .toHex() method from JavaScript, this tool excels in efficiency, offering significant speed improvements compared to native implementations.

Key Features:

  • Blazing Fast Performance: On an M3 Pro MacBook Pro, hextreme demonstrates a performance that's approximately twice as fast as the native .toHex() method in Firefox (version 133b7) and is only about 2.5 times slower than the corresponding method in Safari Tech Preview (version 207). In the Node environment, it's about twice as slow as the native .toString('hex') in Node version 22.11, and five times slower than Bun (version 1.1.34).
  • Optimized for Standard Arrays: While primarily designed for Uint8Arrays, hextreme also efficiently handles standard arrays of numbers ranging from 0 to 255.
  • Memory-efficient Chunks: To mitigate the risk of excessive memory allocations, the implementation processes data in manageable 1MB chunks when using the TextDecoder.
  • Manual Loop Unrolling: By incorporating a method akin to Duff's device, hextreme enhances performance in Chrome, ensuring rapid conversions across different browsers.

Usage Example:

To utilize hextreme in your project, you can run tests and see the impressive performance:

% npm run test

> hextreme@0.1.0 test
> npm run testNode && npm run testBun

> hextreme@0.1.0 testNode
> node test.mjs

Generating random test data ...
Converting to hex ...
Checking results ...
All tests passed :)

Benchmarking 48.6 MiB ...
Buffer.toString: 24.18 ms
toHex: 49.65 ms
_toHexUsingTextDecoder: 50.71 ms
_toHexInChunksUsingTextDecoder: 50.61 ms
_toHexUsingStringConcat: 2035.04 ms

Whether you are a developer seeking faster hex string conversion methods or simply exploring efficient data manipulation techniques, hextreme is crafted to meet your needs effectively!