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

Performance gains for hex and base64 #366

Closed
wants to merge 2 commits into from
Closed

Conversation

jawj
Copy link

@jawj jawj commented Dec 11, 2024

Following up on #364, I created a new package — hextreme — for fast hex and base64 encoding and decoding, and I've updated this library to use it.

The tests are passing, and I've added benchmarks that show performance gains (in Node) of 3 – 13x:

Before:

BrowserBuffer#toString("hex") x 72,483 ops/sec ±0.19% (99 runs sampled)
NodeBuffer#toString("hex") x 527,266 ops/sec ±0.33% (97 runs sampled)
Fastest is NodeBuffer#toString("hex")

BrowserBuffer#from(hexString, "hex") x 54,803 ops/sec ±0.84% (95 runs sampled)
NodeBuffer#from(hexString, "hex") x 342,656 ops/sec ±0.14% (97 runs sampled)
Fastest is NodeBuffer#from(hexString, "hex")

BrowserBuffer#toString("base64") x 30,815 ops/sec ±0.18% (94 runs sampled)
NodeBuffer#toString("base64") x 2,594,667 ops/sec ±0.44% (97 runs sampled)
Fastest is NodeBuffer#toString("base64")

BrowserBuffer#from(base64String, "base64") x 28,184 ops/sec ±0.36% (97 runs sampled)
NodeBuffer#from(base64String, "base64") x 1,218,393 ops/sec ±0.40% (96 runs sampled)
Fastest is NodeBuffer#from(base64String, "base64")

After:

BrowserBuffer#toString("hex") x 348,151 ops/sec ±0.51% (97 runs sampled)
NodeBuffer#toString("hex") x 545,605 ops/sec ±0.60% (96 runs sampled)
Fastest is NodeBuffer#toString("hex")

BrowserBuffer#from(hexString, "hex") x 187,737 ops/sec ±0.29% (95 runs sampled)
NodeBuffer#from(hexString, "hex") x 348,078 ops/sec ±0.24% (98 runs sampled)
Fastest is NodeBuffer#from(hexString, "hex")

BrowserBuffer#toString("base64") x 404,447 ops/sec ±0.27% (98 runs sampled)
NodeBuffer#toString("base64") x 2,658,187 ops/sec ±0.42% (97 runs sampled)
Fastest is NodeBuffer#toString("base64")

BrowserBuffer#from(base64String, "base64") x 92,365 ops/sec ±0.45% (98 runs sampled)
NodeBuffer#from(base64String, "base64") x 1,167,713 ops/sec ±1.30% (92 runs sampled)
Fastest is NodeBuffer#from(base64String, "base64")

Please let me know if you need anything else in order to be able to merge this.

@jawj
Copy link
Author

jawj commented Dec 11, 2024

This change should also make it very easy to add support for base64url (#314) since hextreme supports that.

@@ -8,7 +8,7 @@

'use strict'

const base64 = require('base64-js')
const hextreme = require('hextreme')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new dependency as non-battle-tested as a newly published package - unfortunately - isn't tenable for Buffer.

The TextDecoder option presented previously was an easy option as the complexity of review remained in band.

@jawj
Copy link
Author

jawj commented Dec 12, 2024

OK. Kind of disappointing, but I guess I see where you're coming from. I'll start again and probably just do toString('hex') for now.

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

Successfully merging this pull request may close these issues.

2 participants