Skip to content

Instantly share code, notes, and snippets.

@epreston
Created April 30, 2023 05:18
Show Gist options
  • Save epreston/9a65f60517969981d29e8a0d195d5cb3 to your computer and use it in GitHub Desktop.
Save epreston/9a65f60517969981d29e8a0d195d5cb3 to your computer and use it in GitHub Desktop.
javascript - compatibility - determine if an object is transferable to worker thread
function isTranferable(obj) {
try {
return (
obj instanceof ArrayBuffer ||
obj instanceof MessagePort ||
obj instanceof ImageBitmap || // safari 15+ ios 15+
obj instanceof OffscreenCanvas // safari 16.4+ ios 16.4+
);
} catch {
return false;
}
}
@epreston
Copy link
Author

This sidesteps a browser compatibility issue with Safari.

At the time or writing, 16.4 is the recent release introducing support for OffscreenCanvas. Version 15 is still popular, and version 14 is rare but still 3x more common than the latest release.

@allsey87
Copy link

I was hoping for an inbuilt browser function for this, but considering the list of transferable objects is quite short, this seems like a reasonable approach to me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment