Transfer Large Data using Transferable Objects
mainBy default, Penpal uses the structured clone algorithm, which copies data and can double memory usage. To improve performance when sending large buffers, use transferable objects to move memory ownership instead of cloning.
From Caller to Remote
Pass an instance of CallOptions as the last argument to a remote method, setting the transferables option to an array of the objects you wish to transfer (e.g., ArrayBuffers).
From Remote to Caller
When responding to a method call, return an instance of Reply instead of a raw value. Set the transferables option in the Reply constructor to specify which objects should be transferred back to the caller.
// Caller side: transferring a buffer to the remote
const numbersArray = new Int32Array(new ArrayBuffer(8));
const result = await remote.double(
numbersArray,
new CallOptions({ transferables: [numbersArray.buffer] })
);
// Remote side: returning a buffer via Reply
// methods: { double(numbersArray) { ... return new Reply(resultArray, { transferables: [resultArray.buffer] }); } }