Skip to content

Commit

Permalink
benchmark: split Buffer.byteLength benchmark
Browse files Browse the repository at this point in the history
PR-URL: #46616
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
joyeecheung authored and targos committed Mar 13, 2023
1 parent ffcd301 commit 8a0620f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 49 deletions.
22 changes: 22 additions & 0 deletions benchmark/buffers/buffer-bytelength-buffer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
const common = require('../common');

const bench = common.createBenchmark(main, {
len: [2, 16, 256], // x16
n: [4e6],
});

function main({ n, len }) {
const data = Buffer.alloc(len * 16, 'a');
const expected = Buffer.byteLength(data, 'buffer');
let changed = false;
bench.start();
for (let i = 0; i < n; i++) {
const actual = Buffer.byteLength(data, 'buffer');
if (expected !== actual) { changed = true; }
}
bench.end(n);
if (changed) {
throw new Error('Result changed during iteration');
}
}
40 changes: 40 additions & 0 deletions benchmark/buffers/buffer-bytelength-string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';
const common = require('../common');

const bench = common.createBenchmark(main, {
type: ['one_byte', 'two_bytes', 'three_bytes', 'four_bytes'],
encoding: ['utf8', 'base64'],
repeat: [1, 2, 16, 256], // x16
n: [4e6],
});

// 16 chars each
const chars = {
one_byte: 'hello brendan!!!',
two_bytes: 'ΰαβγδεζηθικλμνξο',
three_bytes: '挰挱挲挳挴挵挶挷挸挹挺挻挼挽挾挿',
four_bytes: '𠜎𠜱𠝹𠱓𠱸𠲖𠳏𠳕𠴕𠵼𠵿𠸎𠸏𠹷𠺝𠺢',
};

function getInput(type, repeat, encoding) {
const original = (repeat === 1) ? chars[type] : chars[type].repeat(repeat);
if (encoding === 'base64') {
Buffer.from(original, 'utf8').toString('base64');
}
return original;
}

function main({ n, repeat, encoding, type }) {
const data = getInput(type, repeat, encoding);
const expected = Buffer.byteLength(data, encoding);
let changed = false;
bench.start();
for (let i = 0; i < n; i++) {
const actual = Buffer.byteLength(data, encoding);
if (expected !== actual) { changed = true; }
}
bench.end(n);
if (changed) {
throw new Error('Result changed during iteration');
}
}
49 changes: 0 additions & 49 deletions benchmark/buffers/buffer-bytelength.js

This file was deleted.

0 comments on commit 8a0620f

Please sign in to comment.