1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-01 09:24:20 -04:00
denoland-deno/cli/tests/core_decode_perf.js
Bartek Iwańczuk dc6e0c3591
feat: Deno.core.{encode,decode}; standalone UTF-8 encoding/decoding (#4349)
This commits add two new methods to "Deno.core" namespace: "encode" and "decode".

Those methods are bound in Rust to provide a) fast b) generally available of encoding and decoding UTF-8 strings. 

Both methods are now used in "cli/js/dispatch_json.ts".
2020-03-15 15:31:55 +01:00

37 lines
889 B
JavaScript

const mixed = new TextEncoder().encode("@Ā๐😀");
function generateRandom(bytes) {
const result = new Uint8Array(bytes);
let i = 0;
while (i < bytes) {
const toAdd = Math.floor(Math.random() * Math.min(4, bytes - i));
switch (toAdd) {
case 0:
result[i] = mixed[0];
i++;
break;
case 1:
result[i] = mixed[1];
result[i + 1] = mixed[2];
i += 2;
break;
case 2:
result[i] = mixed[3];
result[i + 1] = mixed[4];
result[i + 2] = mixed[5];
i += 3;
break;
case 3:
result[i] = mixed[6];
result[i + 1] = mixed[7];
result[i + 2] = mixed[8];
result[i + 3] = mixed[9];
i += 4;
break;
}
}
return result;
}
const randomData = generateRandom(1024);
for (let i = 0; i < 10_000; i++) Deno.core.decode(randomData);