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

benchmark: add benchmarks for encodings #50348

Merged
merged 1 commit into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion benchmark/buffers/buffer-base64-decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,15 @@ function main({ n, size }) {
assert.strictEqual(s.length % 4, 0);
const b = Buffer.allocUnsafe(encodedSize);
b.write(s, 0, encodedSize, 'base64');

let tmp;

bench.start();
for (let i = 0; i < n; i += 1) b.base64Write(s, 0, s.length);

for (let i = 0; i < n; i += 1)
tmp = b.base64Write(s, 0, s.length);

bench.end(n);

assert.strictEqual(tmp, encodedSize);
}
11 changes: 10 additions & 1 deletion benchmark/buffers/buffer-base64-encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

'use strict';
const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
len: [64 * 1024 * 1024],
Expand All @@ -35,7 +36,15 @@ function main({ n, len }) {
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) b.toString('base64');

for (i = 0; i < n; ++i)
tmp = b.toString('base64');

bench.end(n);

assert.strictEqual(typeof tmp, 'string');
}
29 changes: 29 additions & 0 deletions benchmark/buffers/buffer-base64url-decode.js
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);
}
29 changes: 29 additions & 0 deletions benchmark/buffers/buffer-base64url-encode.js
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');
}
29 changes: 29 additions & 0 deletions benchmark/buffers/buffer-hex-decode.js
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');
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const common = require('../common.js');
const assert = require('assert');

const bench = common.createBenchmark(main, {
len: [64, 1024],
Expand All @@ -14,11 +15,14 @@ function main({ len, n }) {
buf[i] = i & 0xff;

const hex = buf.toString('hex');
let tmp;

bench.start();

for (let i = 0; i < n; i += 1)
Buffer.from(hex, 'hex');
tmp = Buffer.from(hex, 'hex');

bench.end(n);

assert.strictEqual(typeof tmp, 'object');
}