-
-
Notifications
You must be signed in to change notification settings - Fork 229
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
a ~9x faster .toString('hex') #219
Comments
Make a pull request... if you can still beat the benchmarks in the actual code, then power to it 👍 |
Oh, so someone beat me to it... |
but mine was still faster :)
Guess it is cuz i have a larger lookup table (using all 256), hex-string lookup table is just 16 (0-f), so they lookup 1 byte twice to get both letters in order to get one hex value |
@jimmywarting nice! |
Happy to include this approach in |
@feross do you prefer include predefined table for lookup or compute it on each |
@fanatid If the predefined table has 256 entries, I'd rather compute it. |
I'll check it shortly and will try send PR. |
Lookup table faster ~9-12 times than toString('hex') for each byte. See feross#219
I was reading upon MDN and saw there example of how they take a buffer and make it to a hex string.
There solution was: They used DataView got 4 bytes as Uint32 to reduce the iterations by 4.
I thought, "how much faster is it as compared to a simple for loop like your hexSlice or a
buf.reduce(fn, '')
?I tested a few implementations with a 60k random buffer and 100 iteration and the test where roughly the same.
i figure, can i beat that DataView somehow? then it hit me! a lookup table!
the table lookup where ~8.25 times faster then using DataView and ~9.2 times faster then
buffer.toString('hex')
Here is the complete test if you are interested or would like to test it yourself
I'm thinking whether or not i should publish it on NPM. couldn't find something like it.
If you like you can embed this bytesToHex function into your lib if you feel like it.
If you want me to publish it on npm and you would just like to require it i can do that too
The text was updated successfully, but these errors were encountered: