Skip to content

Commit

Permalink
perf: add performance test for TextEncoder.encodeInto
Browse files Browse the repository at this point in the history
  • Loading branch information
tarruda committed Apr 11, 2021
1 parent 8aa0d5f commit fccede4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cli/bench/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ const EXEC_TIME_BENCHMARKS: &[(&str, &[&str], Option<i32>)] = &[
&["run", "cli/tests/text_encoder_perf.js"],
None,
),
(
"text_encoder_into",
&["run", "cli/tests/text_encoder_into_perf.js"],
None,
),
(
"check",
&[
Expand Down
34 changes: 34 additions & 0 deletions cli/tests/text_encoder_into_perf.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit fccede4

Please sign in to comment.