From 81063cc3e54d060dd72cfad599dfa618730799d4 Mon Sep 17 00:00:00 2001 From: Thiago Padilha Date: Sat, 10 Apr 2021 22:42:00 -0300 Subject: [PATCH] perf: add performance test for TextEncoder.encodeInto --- cli/bench/main.rs | 5 +++++ cli/tests/text_encoder_into_perf.js | 34 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 cli/tests/text_encoder_into_perf.js diff --git a/cli/bench/main.rs b/cli/bench/main.rs index 8890ec79cebd41..1faef97dc80b91 100644 --- a/cli/bench/main.rs +++ b/cli/bench/main.rs @@ -89,6 +89,11 @@ const EXEC_TIME_BENCHMARKS: &[(&str, &[&str], Option)] = &[ &["run", "cli/tests/text_encoder_perf.js"], None, ), + ( + "text_encoder_into", + &["run", "cli/tests/text_encoder_into_perf.js"], + None, + ), ( "check", &[ diff --git a/cli/tests/text_encoder_into_perf.js b/cli/tests/text_encoder_into_perf.js new file mode 100644 index 00000000000000..8d60e9f000d3a8 --- /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);