Skip to content

Build your personal knowledge base with Trilium Notes

perf(sync): use native Uint8Array base64 in the browser provider

Elian Dorana99e96ce24af
diff --git a/apps/standalone/src/lightweight/crypto_provider.spec.ts b/apps/standalone/src/lightweight/crypto_provider.spec.ts
index a21e754b3d..3283f54a51 100644
--- a/apps/standalone/src/lightweight/crypto_provider.spec.ts
+++ b/apps/standalone/src/lightweight/crypto_provider.spec.ts
@@ -1,4 +1,4 @@
-import { describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it } from "vitest";
import aesjs from "aes-js";
import BrowserCryptoProvider from "./crypto_provider.js";
@@ -155,4 +155,34 @@ describe("BrowserCryptoProvider base64", () => {
const backing = new Uint8Array([0xff, 0xff, 0x66, 0x6f, 0x6f, 0xff]); // "foo" in the middle
expect(provider.base64Encode(backing.subarray(2, 5))).toBe("Zm9v");
});
+
+ describe("native (TC39 arraybuffer-base64) fast path", () => {
+ // The test runtime may or may not ship the native methods; save whatever is there and
+ // restore it afterwards, so stubbing never clobbers a real implementation.
+ /* eslint-disable @typescript-eslint/no-explicit-any */
+ const proto = Uint8Array.prototype as any;
+ const ctor = Uint8Array as any;
+ const savedToBase64 = proto.toBase64;
+ const savedFromBase64 = ctor.fromBase64;
+
+ afterEach(() => {
+ if (savedToBase64 === undefined) delete proto.toBase64;
+ else proto.toBase64 = savedToBase64;
+ if (savedFromBase64 === undefined) delete ctor.fromBase64;
+ else ctor.fromBase64 = savedFromBase64;
+ });
+
+ it("prefers Uint8Array.prototype.toBase64 for encoding when the runtime provides it", () => {
+ proto.toBase64 = function (this: Uint8Array) {
+ return `NATIVE(${this.length})`;
+ };
+ expect(provider.base64Encode(new Uint8Array(3))).toBe("NATIVE(3)");
+ });
+
+ it("prefers Uint8Array.fromBase64 for decoding when the runtime provides it", () => {
+ ctor.fromBase64 = (base64: string) => new Uint8Array([base64.length]);
+ expect(Array.from(provider.base64Decode("abcd"))).toEqual([4]);
+ });
+ /* eslint-enable @typescript-eslint/no-explicit-any */
+ });
});
diff --git a/apps/standalone/src/lightweight/crypto_provider.ts b/apps/standalone/src/lightweight/crypto_provider.ts
index 3712d4adfc..e7a41b493f 100644
--- a/apps/standalone/src/lightweight/crypto_provider.ts
+++ b/apps/standalone/src/lightweight/crypto_provider.ts
@@ -96,8 +96,16 @@ export default class BrowserCryptoProvider implements CryptoProvider {
}
base64Encode(bytes: Uint8Array): string {
- // Build the binary string in 32K chunks via fromCharCode.apply. This avoids both the
- // pathological per-byte string concatenation of a naive loop and the call-stack limit
+ // Prefer the native (TC39 arraybuffer-base64) encoder where available — Chrome 140+,
+ // Firefox 133+, Safari 18.2+. It runs at native speed (SIMD) and avoids materializing
+ // the intermediate "binary string" entirely. Detected per call so tests can stub it.
+ const nativeBytes = bytes as NativeBase64Array;
+ if (typeof nativeBytes.toBase64 === "function") {
+ return nativeBytes.toBase64();
+ }
+
+ // Fallback: build the binary string in 32K chunks via fromCharCode.apply. This avoids both
+ // the pathological per-byte string concatenation of a naive loop and the call-stack limit
// of applying fromCharCode to the whole array at once.
const CHUNK = 0x8000; // 32768
let binary = "";
@@ -108,6 +116,11 @@ export default class BrowserCryptoProvider implements CryptoProvider {
}
base64Decode(base64: string): Uint8Array {
+ const nativeCtor = Uint8Array as unknown as NativeBase64Constructor;
+ if (typeof nativeCtor.fromBase64 === "function") {
+ return nativeCtor.fromBase64(base64);
+ }
+
const binary = atob(base64);
const len = binary.length;
const bytes = new Uint8Array(len);
@@ -118,6 +131,17 @@ export default class BrowserCryptoProvider implements CryptoProvider {
}
}
+/**
+ * Structural types for the TC39 "arraybuffer-base64" Uint8Array methods, which are newer than the
+ * ES2022 lib this package compiles against. Both are probed with `typeof` before use.
+ */
+interface NativeBase64Array extends Uint8Array {
+ toBase64?(): string;
+}
+interface NativeBase64Constructor {
+ fromBase64?(base64: string): Uint8Array;
+}
+
/**
* A synchronous cipher implementation using aes-js.
* Matches Node.js crypto behavior with update() and final() methods.