diff options
author | Bartek Iwańczuk <biwanczuk@gmail.com> | 2020-03-15 15:31:55 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-15 15:31:55 +0100 |
commit | dc6e0c3591709d6f8887bb672af1de54dfc8a974 (patch) | |
tree | 0239d62e419b840f1c4e5cd631a7b87689ed2a3b /cli | |
parent | ec3f44581bf4312cbbe36b71daca7f0474177cf3 (diff) |
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".
Diffstat (limited to 'cli')
-rw-r--r-- | cli/js/globals.ts | 3 | ||||
-rw-r--r-- | cli/js/ops/dispatch_json.ts | 5 | ||||
-rw-r--r-- | cli/tests/core_decode_perf.js | 37 | ||||
-rw-r--r-- | cli/tests/core_encode_perf.js | 32 |
4 files changed, 74 insertions, 3 deletions
diff --git a/cli/js/globals.ts b/cli/js/globals.ts index 8d122878f..21ce7e619 100644 --- a/cli/js/globals.ts +++ b/cli/js/globals.ts @@ -97,6 +97,9 @@ declare global { evalContext(code: string): [any, EvalErrorInfo | null]; formatError: (e: Error) => string; + + decode(bytes: Uint8Array): string; + encode(text: string): Uint8Array; } // Only `var` variables show up in the `globalThis` type when doing a global diff --git a/cli/js/ops/dispatch_json.ts b/cli/js/ops/dispatch_json.ts index 4aa1f6b8b..9ff0f13f5 100644 --- a/cli/js/ops/dispatch_json.ts +++ b/cli/js/ops/dispatch_json.ts @@ -1,6 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. import * as util from "../util.ts"; -import { TextEncoder, TextDecoder } from "../web/text_encoding.ts"; import { core } from "../core.ts"; import { OPS_CACHE } from "../runtime.ts"; import { ErrorKind, getErrorClass } from "../errors.ts"; @@ -30,13 +29,13 @@ function nextPromiseId(): number { } function decode(ui8: Uint8Array): JsonResponse { - const s = new TextDecoder().decode(ui8); + const s = core.decode(ui8); return JSON.parse(s) as JsonResponse; } function encode(args: object): Uint8Array { const s = JSON.stringify(args); - return new TextEncoder().encode(s); + return core.encode(s); } function unwrapResponse(res: JsonResponse): Ok { diff --git a/cli/tests/core_decode_perf.js b/cli/tests/core_decode_perf.js new file mode 100644 index 000000000..fc00b8996 --- /dev/null +++ b/cli/tests/core_decode_perf.js @@ -0,0 +1,37 @@ +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); diff --git a/cli/tests/core_encode_perf.js b/cli/tests/core_encode_perf.js new file mode 100644 index 000000000..5cde81c7a --- /dev/null +++ b/cli/tests/core_encode_perf.js @@ -0,0 +1,32 @@ +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); +for (let i = 0; i < 10_000; i++) Deno.core.encode(randomData); |