2022-11-07 04:27:41 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const common = require('../common.js');
|
|
|
|
|
|
|
|
const bench = common.createBenchmark(main, {
|
|
|
|
encoding: ['utf-8', 'latin1', 'iso-8859-3'],
|
|
|
|
ignoreBOM: [0, 1],
|
2022-12-16 07:11:07 +08:00
|
|
|
fatal: [0, 1],
|
2022-11-07 04:27:41 +08:00
|
|
|
len: [256, 1024 * 16, 1024 * 512],
|
2022-11-07 22:07:09 +08:00
|
|
|
n: [1e2],
|
2023-02-03 18:42:25 +08:00
|
|
|
type: ['SharedArrayBuffer', 'ArrayBuffer', 'Buffer'],
|
2022-11-07 04:27:41 +08:00
|
|
|
});
|
|
|
|
|
2022-12-16 07:11:07 +08:00
|
|
|
function main({ encoding, len, n, ignoreBOM, type, fatal }) {
|
|
|
|
const decoder = new TextDecoder(encoding, { ignoreBOM, fatal });
|
2022-11-07 22:07:09 +08:00
|
|
|
let buf;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case 'SharedArrayBuffer': {
|
|
|
|
buf = new SharedArrayBuffer(len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'ArrayBuffer': {
|
|
|
|
buf = new ArrayBuffer(len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'Buffer': {
|
|
|
|
buf = Buffer.allocUnsafe(len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-11-07 04:27:41 +08:00
|
|
|
|
|
|
|
bench.start();
|
2022-11-07 22:06:54 +08:00
|
|
|
for (let i = 0; i < n; i++) {
|
2022-12-16 07:11:07 +08:00
|
|
|
try {
|
|
|
|
decoder.decode(buf);
|
|
|
|
} catch {
|
|
|
|
// eslint-disable no-empty
|
|
|
|
}
|
2022-11-07 22:06:54 +08:00
|
|
|
}
|
2022-11-07 04:27:41 +08:00
|
|
|
bench.end(n);
|
|
|
|
}
|