Skip to content

Commit 709c84e

Browse files
authored
Optimize transferables detection
1 parent 02a896d commit 709c84e

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

dist/comlink.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -289,35 +289,27 @@ function cbProxy(cb, callPath = [], target = function () { }) {
289289
}
290290
});
291291
}
292-
function isTransferable(thing) {
293-
return TRANSFERABLE_TYPES.some(type => thing instanceof type);
294-
}
295-
function* iterateAllProperties(value, path = [], visited = null) {
296-
if (!value)
297-
return;
298-
if (!visited)
299-
visited = new WeakSet();
300-
if (visited.has(value))
301-
return;
302-
if (typeof value === "string")
292+
function findTransferables(value, accumulator) {
293+
if (typeof value !== "object" || value==null) {
303294
return;
304-
if (typeof value === "object")
305-
visited.add(value);
295+
}
306296
if (ArrayBuffer.isView(value)) {
307-
yield { value: value.buffer, path: [...path, 'buffer'] };
297+
accumulator.push(value.buffer);
308298
return;
309299
}
310-
yield { value, path };
311-
const keys = Object.keys(value);
312-
for (const key of keys)
313-
yield* iterateAllProperties(value[key], [...path, key], visited);
300+
for (const type of TRANSFERABLE_TYPES) {
301+
if (value instanceof type) {
302+
accumulator.push(value);
303+
return;
304+
}
305+
}
306+
for (let key in obj) {
307+
findTransferables(obj[key], accumulator);
308+
}
314309
}
315310
function transferableProperties(obj) {
316-
const r = [];
317-
for (const prop of iterateAllProperties(obj)) {
318-
if (isTransferable(prop.value))
319-
r.push(prop.value);
320-
}
311+
const accumulator = [];
312+
findTransferables(obj, accumulator);
321313
return r;
322314
}
323315
function makeInvocationResult(obj) {

0 commit comments

Comments
 (0)