Created
April 30, 2023 05:18
-
-
Save epreston/9a65f60517969981d29e8a0d195d5cb3 to your computer and use it in GitHub Desktop.
javascript - compatibility - determine if an object is transferable to worker thread
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
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
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.