diff --git a/cli/bench/main.rs b/cli/bench/main.rs index f2ade54d80142f..b98a9d141e4e2e 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);