summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/internal_binding
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2024-07-30 05:39:55 -0700
committerGitHub <noreply@github.com>2024-07-30 18:09:55 +0530
commit1ba88a7892fa1b0d7cf229b0cd5709575901ebd0 (patch)
treeabb158d0bc7a927c7c387bed25aae9faf7e0a7e4 /ext/node/polyfills/internal_binding
parentc6ecf70a0963ce5cac67af97c55aa360a1d25c4b (diff)
perf(ext/node): improve `Buffer` from string performance (#24567)
Fixes https://github.com/denoland/deno/issues/24323 - Use a Buffer pool for `fromString` - Implement fast call base64 writes - Direct from string `create` method for each encoding op ``` $ deno bench -A bench.mjs # 1.45.1+fee4d3a cpu: Apple M1 Pro runtime: deno 1.45.1+fee4d3a (aarch64-apple-darwin) benchmark time (avg) (min … max) p75 p99 p999 ----------------------------------------------------------- ----------------------------- Buffer.from base64 550 ns/iter (490 ns … 1'265 ns) 572 ns 606 ns 1'265 ns Buffer#write base64 285 ns/iter (259 ns … 371 ns) 307 ns 347 ns 360 ns $ ~/gh/deno/target/release/deno bench -A bench.mjs # this PR cpu: Apple M1 Pro runtime: deno dev (aarch64-apple-darwin) benchmark time (avg) (min … max) p75 p99 p999 ----------------------------------------------------------- ----------------------------- Buffer.from base64 151 ns/iter (145 ns … 770 ns) 148 ns 184 ns 648 ns Buffer#write base64 62.58 ns/iter (60.79 ns … 157 ns) 61.65 ns 75.79 ns 141 ns $ node bench.mjs # v22.4.0 cpu: Apple M1 Pro runtime: node v22.4.0 (arm64-darwin) benchmark time (avg) (min … max) p75 p99 p999 ----------------------------------------------------------- ----------------------------- Buffer.from base64 163 ns/iter (96.92 ns … 375 ns) 99.45 ns 127 ns 220 ns Buffer#write base64 75.48 ns/iter (74.97 ns … 134 ns) 75.17 ns 81.83 ns 96.84 ns ```
Diffstat (limited to 'ext/node/polyfills/internal_binding')
-rw-r--r--ext/node/polyfills/internal_binding/_utils.ts17
1 files changed, 17 insertions, 0 deletions
diff --git a/ext/node/polyfills/internal_binding/_utils.ts b/ext/node/polyfills/internal_binding/_utils.ts
index 74dc3cbcd..0b1e1709d 100644
--- a/ext/node/polyfills/internal_binding/_utils.ts
+++ b/ext/node/polyfills/internal_binding/_utils.ts
@@ -7,6 +7,7 @@ import {
forgivingBase64Decode,
forgivingBase64UrlDecode,
} from "ext:deno_web/00_infra.js";
+import { op_base64_write } from "ext:core/ops";
export function asciiToBytes(str: string) {
const length = str.length;
@@ -27,6 +28,22 @@ export function base64ToBytes(str: string) {
}
}
+export function base64Write(
+ str: string,
+ buffer: Uint8Array,
+ offset: number = 0,
+ length?: number,
+): number {
+ length = length ?? buffer.byteLength - offset;
+ try {
+ return op_base64_write(str, buffer, offset, length);
+ } catch {
+ str = base64clean(str);
+ str = str.replaceAll("-", "+").replaceAll("_", "/");
+ return op_base64_write(str, buffer, offset, length);
+ }
+}
+
const INVALID_BASE64_RE = /[^+/0-9A-Za-z-_]/g;
function base64clean(str: string) {
// Node takes equal signs as end of the Base64 encoding