edgedb / edgedb-js

The official TypeScript/JS client library and query builder for EdgeDB
https://edgedb.com
Apache License 2.0
508 stars 65 forks source link

If you want to make EdgeDB's base64 encode function faster & use less memory #674

Closed Jarred-Sumner closed 9 months ago

Jarred-Sumner commented 1 year ago

Change this code: https://github.com/edgedb/edgedb-js/blob/b2d9ef57a9efddcc3737ecd9709533650f09ac36/packages/driver/src/primitives/buffer.ts#L35-L44

To something more like:

if (typeof Buffer === "function") {
  decodeB64 = (b64: string): Uint8Array => {
    // @ts-ignore
    return Buffer.from(b64, "base64");
  };
  encodeB64 = (data: Uint8Array): string => {
    // @ts-ignore
    //  - Avoid cloning the contents of Buffer
    //  - Avoid creating a new Buffer if it is already a Buffer instance
    const buffer = !Buffer.isBuffer(data) ? Buffer.from(data.buffer, data.byteOffset, data.byteLength) : data;
    return buffer.toString("base64");
  };
} 
scotttrinh commented 1 year ago

Thanks for the tip @Jarred-Sumner! I'll take a look. Love these easy wins.