Skip to content

Commit

Permalink
benchmark: add text-encoder benchmark
Browse files Browse the repository at this point in the history
PR-URL: #45450
Reviewed-By: Stephen Belanger <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
anonrig authored and danielleadams committed Jan 3, 2023
1 parent 1de1f67 commit fcf6188
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions benchmark/util/text-encoder.js
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit fcf6188

Please sign in to comment.