summaryrefslogtreecommitdiff
path: root/benchmark/util/text-encoder.js
blob: ca3cb827779be31e2c64e01f725e05a1b7266420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);
}