Search code examples
javascripttyped-arrays

How would you explain Javascript Typed Arrays to someone with no programming experience outside of Javascript?


I have been messing with Canvas a lot lately, developing some ideas I have for a web-based game. As such I've recently run into Javascript Typed Arrays. I've done some reading for example at MDN and I just can't understand anything I'm finding. It seems most often, when someone is explaining Typed Arrays, they use analogies to other languages that are a little beyond my understanding.

My experience with "programming," if you can call it that (and not just front-end scripting), is pretty much limited to Javascript. I do feel as though I understand Javascript pretty well outside of this instance, however. I have deeply investigated and used the Object.prototype structure of Javascript, and more subtle factors such as variable referencing and the value of this, but when I look at any information I've found about Typed Arrays, I'm just lost.

With this frame-of-reference in mind, can you describe Typed Arrays in a simple, usable way? The most effective depicted use-case, for me, would be something to do with Canvas image data. Also, a well-commented Fiddle would be most appreciated.


Solution

  • In typed programming languages (to which JavaScript kinda belongs) we usually have variables of fixed declared type that can be dynamically assigned values.

    With Typed Arrays it's quite the opposite.

    You have a fixed chunk of data (represented by ArrayBuffer) that you do not access directly. Instead this data is accessed by views. Views are created at run time and they effectively declare some portion of the buffer to be of a certain type. These views are sub-classes of ArrayBufferView. The views define the certain continuous portion of this chunk of data as elements of an array of a certain type. Once the type is declared browser knows the length and content of each element, as well as a number of such elements. With this knowledge browsers can access individual elements much more efficiently.

    So we dynamically assigning a type to a portion of what actually is just a buffer. We can assign multiple views to the same buffer.

    From the Specs:

    Multiple typed array views can refer to the same ArrayBuffer, of different types,
    lengths, and offsets. 
    
    This allows for complex data structures to be built up in the ArrayBuffer.
    

    As an example, given the following code:

          // create an 8-byte ArrayBuffer
          var b = new ArrayBuffer(8);
    
          // create a view v1 referring to b, of type Int32, starting at
          // the default byte index (0) and extending until the end of the buffer
          var v1 = new Int32Array(b);
    
          // create a view v2 referring to b, of type Uint8, starting at
          // byte index 2 and extending until the end of the buffer
          var v2 = new Uint8Array(b, 2);
    
          // create a view v3 referring to b, of type Int16, starting at
          // byte index 2 and having a length of 2
          var v3 = new Int16Array(b, 2, 2);
    
    The following buffer and view layout is created:
    

    enter image description here

    This defines an 8-byte buffer b, and three views of that buffer, v1, v2, and v3. Each of the views refers to the same buffer -- so v1[0] refers to bytes 0..3 as a signed 32-bit integer, v2[0] refers to byte 2 as a unsigned 8-bit integer, and v3[0] refers to bytes 2..3 as a signed 16-bit integer. Any modification to one view is immediately visible in the other: for example, after v2[0] = 0xff; v21 = 0xff; then v3[0] == -1 (where -1 is represented as 0xffff).

    So instead of declaring data structures and filling them with data, we take data and overlay it with different data types.