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

@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
9 checks passed
@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

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.

3 participants