test(base64): Improve ad-hoc benchmark
MODIFY
packages/base64/test/bench-main.js
packages/base64/test/bench-main.js
-import { encodeBase64, decodeBase64 } from '../src/main';
+// This is a quick and dirty benchmark to encode and decode base64, intended to
+// be run manually to measure performance progress or regressions.
+// This is a hand-rolled benchmark that attempts as much as possible to avoid
+// incorporating the noise of function calls and the underlying Date.now()
+// syscall, by exponentially probing for the number of operations that can be
+// executed within each benchmark's deadline.
-const string = new Array(100)
+import { encodeBase64, decodeBase64 } from '../src/main.js';
+
+const timeout = 1000; // ms
+
+const string = new Array(10000)
.fill(
'there once a rich man from nottingham who tried to cross the river. what a dope, he tripped on a rope. now look at him shiver.',
)
@@ -9,9 +18,9 @@ const data = encodeBase64(string);
{
const start = Date.now();
- const limit = start + 1000;
+ const deadline = start + timeout / 2;
let operations = 0;
- for (let n = 1; Date.now() < limit; n *= 2) {
+ for (let n = 1; Date.now() < deadline; n *= 2) {
for (let i = 0; i < n; i += 1) {
encodeBase64(string);
}
@@ -20,17 +29,17 @@ const data = encodeBase64(string);
const end = Date.now();
const duration = end - start;
console.log(
- operations / duration,
- 'encodes per millisecond, for input of length',
- string.length,
+ 'encodes',
+ (operations * string.length) / duration,
+ 'characters per millisecond',
);
}
{
const start = Date.now();
- const limit = start + 1000;
+ const deadline = start + timeout / 2;
let operations = 0;
- for (let n = 1; Date.now() < limit; n *= 2) {
+ for (let n = 1; Date.now() < deadline; n *= 2) {
for (let i = 0; i < n; i += 1) {
decodeBase64(data);
}
@@ -39,8 +48,8 @@ const data = encodeBase64(string);
const end = Date.now();
const duration = end - start;
console.log(
- operations / duration,
- 'decodes per millisecond, for input of length',
- data.length,
+ 'decodes',
+ (operations * data.length) / duration,
+ 'bytes per millisecond',
);
}

test(base64): Improve ad-hoc benchmark