0

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 1
    It is that you can use `ArrayBuffer.isView` method. If `ArrayBuffer.isView` should return true for any typed array, and you need a `SharedArrayBuffer.isView` method that should return true for any typed array, then you have the very same exact functionality. – Jorge Fuentes González May 07 '23 at 09:37

1 Answers1

0

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375