benchmark: add text-encoder benchmark

PR-URL: https://github.com/nodejs/node/pull/45450
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Yagiz Nizipli
2022-11-18 14:48:01 -05:00
committed by GitHub
parent 1f63c953e6
commit cb96a130a5

View File

@@ -0,0 +1,32 @@
'use strict';
const common = require('../common.js');
const BASE = 'string\ud801';
const bench = common.createBenchmark(main, {
len: [256, 1024, 1024 * 32],
n: [1e4],
op: ['encode', 'encodeInto']
});
function main({ n, op, len }) {
const encoder = new TextEncoder();
const input = BASE.repeat(len);
const subarray = new Uint8Array(len);
bench.start();
switch (op) {
case 'encode': {
for (let i = 0; i < n; i++)
encoder.encode(input);
break;
}
case 'encodeInto': {
for (let i = 0; i < n; i++)
encoder.encodeInto(input, subarray);
break;
}
}
bench.end(n);
}