From 2690458187557632e83eb72313cb9b614abc72d1 Mon Sep 17 00:00:00 2001 From: Barak Date: Tue, 26 Sep 2023 00:51:29 +0300 Subject: [PATCH] refactor: 3x performance improvement for findTransferables --- .../web-renderer/raw/WorkletProcessor.js | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/js/packages/web-renderer/raw/WorkletProcessor.js b/js/packages/web-renderer/raw/WorkletProcessor.js index b88f16e..3bc541a 100644 --- a/js/packages/web-renderer/raw/WorkletProcessor.js +++ b/js/packages/web-renderer/raw/WorkletProcessor.js @@ -16,20 +16,23 @@ const EventTypes = { // Right now we're only looking for the ArrayBuffers behind Float32Array instances as that's // the only type of transferable object that the native engine delivers, but this could be // extended to other types easily. -function findTransferables(val) { +function findTransferables(val, transferables = []) { + if (val instanceof Float32Array) { - return [val.buffer]; - } - - if (typeof val === 'object') { + transferables.push(val.buffer); + } else if (typeof val === 'object') { if (Array.isArray(val)) { - return Array.prototype.concat.apply([], val.map(findTransferables)); + for (let i = 0; i < val.length; ++i) { + findTransferables(val[i], transferables); + } + } else if (val !== null) { + for (let key of Object.keys(val)) { + findTransferables(val[key], transferables); + } } - - return Array.prototype.concat.apply([], Object.keys(val).map(key => findTransferables(val[key]))); } - return []; + return transferables; } class ElementaryAudioWorkletProcessor extends AudioWorkletProcessor {