diff options
author | Thiago Padilha <thiago@padilha.cc> | 2021-05-08 18:31:40 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-08 23:31:40 +0200 |
commit | 18a684ab1c20914e13c27bc10e20bda6396ea38d (patch) | |
tree | 42389bca4d7ec23ac3e310bf4cb99478815b4a77 /cli/tests/text_encoder_into_perf.js | |
parent | a051a7f7bc8dab2a4360c146d08b549cbcf17b8d (diff) |
fix: TextEncoder#encodeInto spec compliance + perf gains (#10129)
Diffstat (limited to 'cli/tests/text_encoder_into_perf.js')
-rw-r--r-- | cli/tests/text_encoder_into_perf.js | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/cli/tests/text_encoder_into_perf.js b/cli/tests/text_encoder_into_perf.js new file mode 100644 index 000000000..8d60e9f00 --- /dev/null +++ b/cli/tests/text_encoder_into_perf.js @@ -0,0 +1,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); |