summaryrefslogtreecommitdiff
path: root/cli/tests/text_encoder_into_perf.js
blob: 8d60e9f000d3a86ae7e4fde496bb44e5ff031090 (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
const mixed = "@Ā๐😀";

function generateRandom(bytes) {
  let result = "";
  let i = 0;
  while (i < bytes) {
    const toAdd = Math.floor(Math.random() * Math.min(4, bytes - i));
    switch (toAdd) {
      case 0:
        result += mixed[0];
        i++;
        break;
      case 1:
        result += mixed[1];
        i++;
        break;
      case 2:
        result += mixed[2];
        i++;
        break;
      case 3:
        result += mixed[3];
        result += mixed[4];
        i += 2;
        break;
    }
  }
  return result;
}

const randomData = generateRandom(1024);
const encoder = new TextEncoder();
const targetBuffer = new Uint8Array(randomData.length * 4);
for (let i = 0; i < 10_000; i++) encoder.encodeInto(randomData, targetBuffer);