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

perf base64url #3682

Merged
merged 2 commits into from
Oct 2, 2023
Merged

perf base64url #3682

merged 2 commits into from
Oct 2, 2023

Conversation

mimiMonads
Copy link
Contributor

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 end

It's proximally 15% faster than the current implementation

@mimiMonads mimiMonads requested a review from kt3k as a code owner September 30, 2023 12:02
@CLAassistant
Copy link

CLAassistant commented Sep 30, 2023

CLA assistant check
All committers have signed the CLA.

Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

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

LGTM

@kt3k
Copy link
Member

kt3k commented Oct 2, 2023

It might be also worth considering using a pattern like b64.replace(/[\+\/\=]/g, (c) => {...})

@kt3k kt3k merged commit 4fe9fd6 into denoland:main Oct 2, 2023
@mimiMonads
Copy link
Contributor Author

I already did, and it was slower, I had another good candidate

@kt3k
Copy link
Member

kt3k commented Oct 3, 2023

Oh, interesting. Thanks for the researches

@ycmjason
Copy link

ycmjason commented Oct 16, 2024

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 slice:

b64.replace(/=?=$/, "").replace(/\+/g, "-").replace(/\//g, "_")

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 😄)

@mimiMonads
Copy link
Contributor Author

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

@ycmjason
Copy link

It's so cool you have found this approach! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants