-
Notifications
You must be signed in to change notification settings - Fork 633
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
perf base64url #3682
perf base64url #3682
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
It might be also worth considering using a pattern like |
I already did, and it was slower, I had another good candidate |
Oh, interesting. Thanks for the researches |
I have just sumble upon this piece of code and found it interesting and ended up at this PR. FWIW... I have done some benchmark and have found that this actually stacks up pretty well with
https://jsbm.dev/Jz884zkOfva86 It is about 1% slower but it could be a good tradeoff for a cleaner implementation. (I understand performance is probably king here 😄) |
Long story short, I usually work with deno, node and bun and after a lot of testing for an impl I just got to the conclusion that that was the fastest way , I mean, sure I don't mind it but I just wanted to share it with the std |
It's so cool you have found this approach! 🎉 |
Reasons for potential speed improvement:
Fewer String Parses: In the original function, the string is parsed three times due to three replace calls. In the new function, the string is parsed at most twice using replace, and even this is optimized using quick endsWith checks.
Optimized Checks for '=': The new function uses the endsWith method, which is optimized for checking the end of a string and is faster than scanning the entire string as in the replace method.
Slice Over Replace: The new function uses slice to remove trailing '=' characters. slice is generally faster for such operations since it simply involves adjusting a pointer/reference to the string start/end, whereas replace would have to parse through the string.
also, this operation is safe because is a base64 where
=
only can be at the endIt's proximally 15% faster than the current implementation