-
-
Notifications
You must be signed in to change notification settings - Fork 234
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
Remove dependency on base64-js
#339
Comments
@jonkoops do we actually need it for browsers in 2024? Could we simply use |
You are correct @dcousens, that is in fact what we did for Keycloak. |
https://nodejs.org/api/globals.html#atobdata was added in Node |
Yeah, this is also an opportunity to align the implementations between Node.js and the browser, might be a good code-cleanup. Did some quick testing, but I was unable to get things to work in a 15 min time-span (had some trouble running the tests on a browser as well). |
I suspect I don't have proof because I haven't benchmarked it, but the implementation in #349 is probably the fastest/best we can get. edit: On second thought, if we optimistically parse base64 with try {
return Buffer.from(atob(str), 'binary');
} catch (e) {
return Buffer.from(atob(removeInvalidCharacters(str)), 'binary')
} I'll have a crack at it at some point. |
Using |
Here are the numbers for
Compared to the implementation in #349:
This was the code used (with the optimized version of function base64Write(buf, string, offset, length) {
return asciiWrite(buf, atob(string), offset, length);
} If you're scratching your head like I was, look no further than the insanely broken implementation of This line in particular is the problem: const index = ArrayPrototypeIndexOf(
kForgivingBase64AllowedChars,
StringPrototypeCharCodeAt(input, n)); In other words, the node.js I'll have to benchmark this in an actual web browser to get some real numbers. |
Oh damn, nice find! I wonder how other run-times such as Bun and Deno compare in this regard. Did you log an issue with the Node.js team? |
What is the browser performance like? We could probably still rely on |
Because Buffer exists as the better alternative (from Node.js' perspective that is). The
Which explains this comment: // The implementation here has not been performance optimized in any way and
// should not be. That said, you could make the argument that this hurts web compatibility, and still file an issue. |
So perhaps take the |
@vweevers, it's still trivial to optimize it with a lookup table. I don't see "legacy" as an excuse to be lazy. Interesting that it mentions the performance issue in the code but not the docs (where it might be the most helpful). In any case, filing an issue to have it optimized now does nothing, because we'll never know whether we're using the optimized version or the unoptimized version. @jonkoops' approach makes more sense. @dcousens, performance in chromium seems optimal. I'm seeing a 1.4x speedup compared to the JS impl. I don't know if I'll get around to testing firefox anytime soon as I don't have good tooling to run FF headless, but I'll start working on a revision of #349 that uses |
I have added a comment in nodejs/node#38433 (comment), and I suspect that's where we can leave it. We are targeting browsers, and the fact that |
Just an update: while using
(Note the numbers differ from the ones in my PR because I ran this on a faster machine) We're basically doing this: function base64Slice (buf, start, end) {
return btoa(latin1Slice(buf, start, end))
} The overhead of |
@chjj I think we can probably say that |
New implementation up at #349 |
I made a PR fixing atob in node.js. Let's see if anything happens. |
Wow their linter is strict about whitespace. Okay, I'm out of energy on that. If they accept it, they accept it, if not, we'll figure something out. |
I have to agree with @dcousens that it's not really an issue that some of these functions are slow in Node.js, it is very clear that compatibility and performance with existing popular web APIs is an afterthought there. And it doesn't really matter anyways, as this library will almost always be used in a web context. Thanks for your hard work @chjj, I appreciate you. |
The
base64-js
package has not been maintained for more than 4 years, so it should probably be removed as a dependency.The text was updated successfully, but these errors were encountered: