summaryrefslogtreecommitdiff
path: root/benchmark/util/text-encoder.js
blob: 707f76df11dc8985e282422f86eba402c01f1111 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
'use strict';

const common = require('../common.js');

const bench = common.createBenchmark(main, {
  len: [16, 32, 256, 1024, 1024 * 32],
  n: [1e4],
  type: ['one-byte-string', 'two-byte-string', 'ascii'],
  op: ['encode', 'encodeInto']
});

function main({ n, op, len, type }) {
  const encoder = new TextEncoder();
  let base = '';

  switch (type) {
    case 'ascii':
      base = 'a';
      break;
    case 'one-byte-string':
      base = '\xff';
      break;
    case 'two-byte-string':
      base = 'ğ';
      break;
  }

  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);
}