javascriptarraybuffersharedarraybuffer

How do I check if a value is a `SharedArrayBuffer` view, in Javascript?


The ArrayBuffer.isView() static method checks if it's one of the ArrayBuffer views, such as TypedArray objects or a DataView.

How do you check if it's a view of a SharedArrayBuffer?

It's been a quite long time since SharedArrayBuffer was introduced, yet oddly, nobody's talking about the SharedArrayBuffer counterpart of the ArrayBuffer.isView method. Or, is it that I can use ArrayBuffer.isView to check for SharedArrayBuffer views as well?

I've read How to check if a variable is a typed array in javascript? but this question seems to be specifically focusing on ArrayBuffers, not SharedArrayBuffers.


Solution

  • You can use instanceof on the .buffer property of the view:

    view.buffer instanceof SharedArrayBuffer
    

    If you are not certain whether the object is a view on any buffer at all, use ArrayBuffer.isView(view) first.