-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
src,lib,buffer: improve atob / btoa performance #38433
Open
XadillaX
wants to merge
10
commits into
nodejs:main
Choose a base branch
from
XadillaX:atob-btoa-perf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+260
−31
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a3f184d
src,lib,buffer: improve atob / btoa performance
XadillaX beceed4
f
XadillaX 5cb24c8
f
XadillaX b42b4ba
f
XadillaX 63b9a49
f
XadillaX ff1d141
f
XadillaX 6b5eeec
f
XadillaX ba56cb9
f
XadillaX 5200196
f
XadillaX 074c82c
f
XadillaX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
const buffer = require('buffer'); | ||
const common = require('../common.js'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
len: [64 * 1024 ], | ||
n: [32] | ||
}, { | ||
test: { len: 256 } | ||
}); | ||
|
||
function main({ n, len }) { | ||
let s = ''; | ||
let large = ''; | ||
let i; | ||
for (i = 0; i < 256; ++i) s += String.fromCharCode(i); | ||
for (i = 0; i < len; i += 256) large += s; | ||
const b64 = btoa(large); | ||
|
||
bench.start(); | ||
for (i = 0; i < n; ++i) buffer.atob(b64); | ||
bench.end(n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
const buffer = require('buffer'); | ||
const common = require('../common.js'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
len: [64 * 1024 * 1024], | ||
n: [32] | ||
}, { | ||
test: { len: 256 } | ||
}); | ||
|
||
function main({ n, len }) { | ||
let s = ''; | ||
let large = ''; | ||
let i; | ||
for (i = 0; i < 256; ++i) s += String.fromCharCode(i); | ||
for (i = 0; i < len; i += 256) large += s; | ||
bench.start(); | ||
for (i = 0; i < n; ++i) buffer.btoa(large); | ||
bench.end(n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
|
||
let s1 = ''; | ||
for (let i = 0; i < 256; ++i) s1 += String.fromCharCode(i); | ||
const s1B64 = Buffer.from(s1).toString('base64'); | ||
|
||
assert.strictEqual(btoa(s1), s1B64); | ||
|
||
const s2 = 'hello world'; | ||
const s2B64 = Buffer.from(s2).toString('base64'); | ||
assert.strictEqual(btoa(s2), s2B64); | ||
|
||
const s3 = 'BlingBling...'; | ||
const s3B64 = Buffer.from(s3).toString('base64'); | ||
assert.strictEqual(btoa(s3), s3B64); | ||
|
||
const s4 = '哇咔咔'; | ||
const s4B64 = Buffer.from(s4).toString('base64'); | ||
assert.throws(() => { btoa(s4); }, { | ||
name: 'InvalidCharacterError', | ||
message: 'Invalid character', | ||
code: 5, | ||
}); | ||
|
||
assert.strictEqual(atob(s1B64), | ||
Buffer.from(s1B64, 'base64').toString('latin1')); | ||
assert.strictEqual(atob(s2B64), | ||
Buffer.from(s2B64, 'base64').toString('latin1')); | ||
assert.strictEqual(atob(s3B64), | ||
Buffer.from(s3B64, 'base64').toString('latin1')); | ||
assert.strictEqual(atob(s4B64), | ||
Buffer.from(s4B64, 'base64').toString('latin1')); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
DOMException are expected in atob/btoa per https://html.spec.whatwg.org/multipage/webappapis.html#dom-atob
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.
It's wrapped in JavaScript side.
btw, this PR seems won't be merged according to @addaleax's suggestion.