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

Improve bufferToHex performance #28

Open
fabiospampinato opened this issue Feb 6, 2022 · 1 comment
Open

Improve bufferToHex performance #28

fabiospampinato opened this issue Feb 6, 2022 · 1 comment
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@fabiospampinato
Copy link

The #7 method mentioned here is a good 10x faster compared to the one currently being used by this library.

That's the code:

// Pre-Init
const LUT_HEX_4b = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
const LUT_HEX_8b = new Array(0x100);
for (let n = 0; n < 0x100; n++) {
  LUT_HEX_8b[n] = `${LUT_HEX_4b[(n >>> 4) & 0xF]}${LUT_HEX_4b[n & 0xF]}`;
}
// End Pre-Init
function toHex(buffer) {
  let out = '';
  for (let idx = 0, edx = buffer.length; idx < edx; idx++) {
    out += LUT_HEX_8b[buffer[idx]];
  }
  return out;
}

Uppercase letters would need to be converted to lowercase though. And a Uint8Array needs to be passed to the function rather than an ArrayBuffer.

@sindresorhus sindresorhus added enhancement New feature or request help wanted Extra attention is needed labels Feb 6, 2022
@fabiospampinato
Copy link
Author

FWIW I've published a little module that does the conversion: https://www.npmjs.com/package/uint8-to-hex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants