|Written by Ian Elliot|
|Tuesday, 23 July 2013|
Page 1 of 2
This is part one of a three part series on typed arrays:
Since then typed arrays have become a way of working with external frameworks of all kind and a way of accessing binary data directly. Before typed arrays the standard way of working with binary data was to store it as a string and convert each character to and from its representation as an integer. Typed arrays make binary data much easier to work with.
The only major problem with typed arrays is that they aren't universally supported. The latest browsers all support it, with IE10 doing some things slightly differently. Older browsers tend not to support it - with IE9 and earlier being the biggest problems.
So if you plan to use typed arrays you need to make sure that you tell users to upgrade to the latest browser if your program doesn't work.
Basic Typed Arrays
The way that typed arrays work is very simple and very powerful and very similar to the way that arrays work in C.
Creating a typed array is just a matter of using the array's constructor. For example:
This creates an array of 10 unsigned 8-bit types. Notice that once created you can use the array as normal, but it doesn't have all of the methods that the Array object does. In this case the array elements are single bytes representing 0 to 255. All typed arrays are initialized to zero when created.
Notice that the array is allocated enough memory to hold all of the elements when it is first constructed. For example:
creates an array of 16-bit unsigned integers and hence allocates 20 bytes of memory to store the entire array.
Using any of the typed arrays follows the same pattern and all you really have to know extra is what is available:
Notice that we have types for one, two and four byte integer values both signed and unsigned and a four and eight byte float.
The only type that might need further explanation is the Uint8ClampedArray. A byte variable has a very small range 0 t0 255 and usually overflow is a problem. There are generally two ways to deal with this either roll over after 255 to 0 or use saturation arithmetic which clamps the value to 255. In other words, Uint8ClampedArray doesn't allow a value bigger than 255 and
IE10 doesn't support clamped arrays.
Notice also that for the unclamped array types the alternative strategy is taken and the value rolls over. For example:
is zero. It also rolls over if you try to assign a value that is too large.
So rollover is the rule for all typed arrays except for clamped.
This is very reasonable, but it can also be confusing. For example, what would you expect the result of
How can this be?
and when you apply the bitwise not you get
which is then treated as a 16-bit signed integer and converted to a standard 64-bit float before being printed, which gives the -256.
Notice that in this case the high order bits are lost if you assign the operation back to an array element of type Uint8Array. That is:
does give the expected result of zero, but only because the 16-bit result is truncated to 8-bits by the type conversion.
|Last Updated ( Tuesday, 20 August 2013 )|