Skip to content

Commit

Permalink
avoid worker data copy when returning values typed arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
jcphill committed Jul 28, 2023
1 parent ad4681a commit fdffef1
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ const window = self;
onmessage = e => {
const data = e.data;
parseData(data).then(result => {
let transferBuffers = [];
if ( result.values && (result.values[0][0].buffer instanceof ArrayBuffer) ) {
transferBuffers = result.values.flat().map(r => r.buffer);
}
if (result._data instanceof ArrayBuffer) {
postMessage(result, [result._data]);
} else {
postMessage(result);
transferBuffers.push(result._data);
}
postMessage(result, transferBuffers);
close();
});
};

0 comments on commit fdffef1

Please sign in to comment.