Game Development Reference
In-Depth Information
•
Uint8Array
: It is an
unsigned integer
, 8 bits long, ranging from 0 to
65,535
•
Uint8ClampedArray
: It is an
unsigned integer
, 8 bits long, ranging
from 0 to 255
•
Int16Array
: It is a
signed integer
, 16 bits long, ranging from
2,147,483,648 to 2,147,483,647
•
Uint16Array
: It is an
unsigned integer
, 16 bits long, ranging from 0
to 4,294,967,295
•
Int32Array
: It is a
signed integer
, 32 bits long, ranging from
9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
•
Uint32Array
: It is an
unsigned integer
, 32 bits long, ranging from 0
to 18,446,744,073,709,551,615
•
Float32Array
: It is a
signed float
, 32 bits long, with a range of 3.4E
+/- 38 (7 digits)
•
Float64Array
: It is a
signed float
, 64 bits long, with a range of 1.7E
+/- 308 (15 digits)
It goes without saying that the larger the view type, the larger the buffer will need
to be to hold the data. Obviously, it follows that the larger the buffer you create, the
more memory the browser will need to set aside for you, whether or not you end up
using that memory. Thus, we should always pay attention to how much memory we
might actually need, and try to allocate no more than that. It would be an awesome
waste of resources to allocate an array of 10,000 elements, each of which are 64 bits
long, just to represent a snake in a game, such as the one that we're building in the
chapter, where the maximum snake size might be no larger than 50 or so elements,
and where each element needs not hold a value larger than say 10.
Given such constraints, we could calculate a rough, yet optimistic array size of 50,
where each element only needs 8 bits (since we'll only need around 10 unique val-
ues). Thus, 50 elements times one byte each, gives us a total buffer size of 50 bytes.
This should be more than enough for our purposes, while the memory consumption
for this buffer alone should stay around 0.05 KB. Not bad.
Finally, you may have noticed, the first part of this section demonstrated typed array
creation without using the
ArrayBuffer
construct explicitly.