Skip to content

Commit e68c850

Browse files
committed
benchmark: add benchmarks for encodings
1 parent 8f742bb commit e68c850

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
const assert = require('assert');
3+
const common = require('../common.js');
4+
5+
const bench = common.createBenchmark(main, {
6+
n: [32],
7+
size: [8 << 20],
8+
});
9+
10+
function main({ n, size }) {
11+
const s = 'abcd'.repeat(size);
12+
const encodedSize = s.length * 3 / 4;
13+
// eslint-disable-next-line node-core/no-unescaped-regexp-dot
14+
s.match(/./); // Flatten string.
15+
assert.strictEqual(s.length % 4, 0);
16+
const b = Buffer.allocUnsafe(encodedSize);
17+
b.write(s, 0, encodedSize, 'base64url');
18+
bench.start();
19+
for (let i = 0; i < n; i += 1) b.base64Write(s, 0, s.length);
20+
bench.end(n);
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../common.js');
3+
4+
const bench = common.createBenchmark(main, {
5+
len: [64 * 1024 * 1024],
6+
n: [32],
7+
}, {
8+
test: { len: 256 },
9+
});
10+
11+
function main({ n, len }) {
12+
const b = Buffer.allocUnsafe(len);
13+
let s = '';
14+
let i;
15+
for (i = 0; i < 256; ++i) s += String.fromCharCode(i);
16+
for (i = 0; i < len; i += 256) b.write(s, i, 256, 'ascii');
17+
bench.start();
18+
for (i = 0; i < n; ++i) b.toString('base64url');
19+
bench.end(n);
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
5+
const bench = common.createBenchmark(main, {
6+
len: [64, 1024],
7+
n: [1e6],
8+
});
9+
10+
function main({ len, n }) {
11+
const buf = Buffer.alloc(len);
12+
13+
for (let i = 0; i < buf.length; i++)
14+
buf[i] = i & 0xff;
15+
16+
const plain = buf;
17+
18+
bench.start();
19+
20+
for (let i = 0; i < n; i += 1)
21+
plain.toString('hex');
22+
23+
bench.end(n);
24+
}

0 commit comments

Comments
Β (0)