-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmark: add benchmarks for encodings
PR-URL: #50348 Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
- Loading branch information
Showing
6 changed files
with
111 additions
and
3 deletions.
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
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,29 @@ | ||
'use strict'; | ||
const assert = require('assert'); | ||
const common = require('../common.js'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
n: [32], | ||
size: [8 << 20], | ||
}); | ||
|
||
function main({ n, size }) { | ||
const s = 'abcd'.repeat(size); | ||
const encodedSize = s.length * 3 / 4; | ||
// eslint-disable-next-line node-core/no-unescaped-regexp-dot | ||
s.match(/./); // Flatten string. | ||
assert.strictEqual(s.length % 4, 0); | ||
const b = Buffer.allocUnsafe(encodedSize); | ||
b.write(s, 0, encodedSize, 'base64url'); | ||
|
||
let tmp; | ||
|
||
bench.start(); | ||
|
||
for (let i = 0; i < n; i += 1) | ||
tmp = b.base64Write(s, 0, s.length); | ||
|
||
bench.end(n); | ||
|
||
assert.strictEqual(tmp, encodedSize); | ||
} |
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,29 @@ | ||
'use strict'; | ||
const common = require('../common.js'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
len: [64 * 1024 * 1024], | ||
n: [32], | ||
}, { | ||
test: { len: 256 }, | ||
}); | ||
|
||
function main({ n, len }) { | ||
const b = Buffer.allocUnsafe(len); | ||
let s = ''; | ||
let i; | ||
for (i = 0; i < 256; ++i) s += String.fromCharCode(i); | ||
for (i = 0; i < len; i += 256) b.write(s, i, 256, 'ascii'); | ||
|
||
let tmp; | ||
|
||
bench.start(); | ||
|
||
for (i = 0; i < n; ++i) | ||
tmp = b.toString('base64url'); | ||
|
||
bench.end(n); | ||
|
||
assert.strictEqual(typeof tmp, 'string'); | ||
} |
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,29 @@ | ||
'use strict'; | ||
|
||
const common = require('../common.js'); | ||
const assert = require('assert'); | ||
|
||
const bench = common.createBenchmark(main, { | ||
len: [64, 1024], | ||
n: [1e6], | ||
}); | ||
|
||
function main({ len, n }) { | ||
const buf = Buffer.alloc(len); | ||
|
||
for (let i = 0; i < buf.length; i++) | ||
buf[i] = i & 0xff; | ||
|
||
const plain = buf; | ||
|
||
bench.start(); | ||
|
||
let tmp; | ||
|
||
for (let i = 0; i < n; i += 1) | ||
tmp = plain.toString('hex'); | ||
|
||
bench.end(n); | ||
|
||
assert.strictEqual(typeof tmp, 'string'); | ||
} |
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