typed array in javascript
Typed array is a view into a buffer.
When creating from an array, the array is copied to a newly created buffer.
When creating from a length, a buffer is allocated, which is initialized with 0s.
When creating from a buffer, the buffer is shared.
- See
./code/typed_array.js
1// https://nodejs.org/api/assert.html
2const assert = require('node:assert');
3
4assert.equal(Int8Array.BYTES_PER_ELEMENT, 1);
5assert.equal(Uint8Array.BYTES_PER_ELEMENT, 1);
6
7assert.equal(Int16Array.BYTES_PER_ELEMENT, 2);
8assert.equal(Uint16Array.BYTES_PER_ELEMENT, 2);
9
10assert.equal(Int32Array.BYTES_PER_ELEMENT, 4);
11assert.equal(Uint32Array.BYTES_PER_ELEMENT, 4);
12
13assert.equal(Float32Array.BYTES_PER_ELEMENT, 4);
14assert.equal(Float64Array.BYTES_PER_ELEMENT, 8);
15
16function test_uint32_array() {
17 let a = new Uint32Array(2);
18 assert.equal(a.length, 2);
19 assert.equal(a[1], 0);
20 a[0] = 0x12345678
21 assert.equal(a[0], 0x12345678);
22
23 // type of buf: ArrayBuffer
24 let buf = a.buffer;
25 assert.equal(buf.byteLength, a.length * Uint32Array.BYTES_PER_ELEMENT);
26 let b = new Uint8Array(buf)
27 assert.equal(b[0], 0x78); // little endian
28 b[1] = 0x23; // also changes a
29 assert.equal(a[0], 0x12342378);
30}
31
32function test_int32_array() {
33 let a = new Int32Array(2);
34 // all typed arrays are 0 initialized
35 assert.equal(a[0], 0);
36 assert.equal(a[1], 0);
37
38 let b = new Int32Array(a); // copy
39 b[0] = 10;
40 assert.equal(a[0], 0);
41
42 let c = Int32Array.from(a);
43 c[0] = 1;
44 assert.equal(a[0], 0);
45
46 let d = Int32Array.of(1, 2, 3);
47 assert.equal(d.length, 3);
48 assert.equal(d[0], 1);
49 assert.equal(d[1], 2);
50 assert.equal(d[2], 3);
51
52 let e = new Int32Array(a.buffer); // share the underlying buffer
53 e[0] = 11;
54 assert.equal(a[0], 11);
55
56 // the from method always copys
57 let f = Int32Array.from(a.buffer); // copy!
58 f[0] = 0;
59 assert.equal(a[0], 11); // a is not changed
60
61 let i = [ 5, 1 ];
62 let g = Int32Array.from(i); // from() always copy
63 assert.equal(g.length, 2);
64 assert.equal(g[0], 5);
65 i[0] = 10; // g is not changed
66 assert.equal(g[0], 5);
67}
68
69function test_int32_array2() {
70 let a = new Int32Array(10);
71 let b = [ 1, 2, 3 ];
72 a.set(b); // copy b to a, start from offset 0
73 assert.equal(a[0], b[0]);
74 assert.equal(a[1], b[1]);
75 assert.equal(a[2], b[2]);
76
77 a.set(b, 2); // copy b to a, start from offset 2
78 assert.equal(a[2], b[0]);
79 assert.equal(a[3], b[1]);
80 assert.equal(a[4], b[2]);
81
82 let c = Int32Array.of(100, 200);
83 a.set(c); // copy c to a, at offset 0
84 assert.equal(a[0], c[0]);
85 assert.equal(a[1], c[1]);
86
87 a = Int32Array.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
88 b = a.slice(1, 3); // copy. start index 1 (included), stop index 3 (excluded)
89 assert.equal(b.length, 2);
90 assert.equal(b[0], a[1]);
91 assert.equal(b[1], a[2]);
92 b[0] = 100;
93 assert.equal(a[1], 1); // a is not changed
94
95 // share memory. start index 1 (included), stop index 3 (excluded)
96 c = a.subarray(1, 3);
97 assert.equal(c.length, 2);
98 assert.equal(c[0], a[1]);
99 assert.equal(c[1], a[2]);
100 c[0] = 100;
101 assert.equal(a[1], 100);
102
103 // slice() copies data!
104 let d = a.slice(1, 3);
105 assert.equal(d.length, 2);
106 assert.equal(d[0], a[1]);
107 assert.equal(d[1], a[2]);
108 d[0] = 1000;
109 assert.equal(a[1], 100); // a is not changed
110}
111
112test_uint32_array();
113test_int32_array();
114test_int32_array2();
./code/int8_array.js
1// https://nodejs.org/api/assert.html
2const assert = require('node:assert');
3
4// From an array
5let a = [ 1, 2, 3 ];
6let b = new Int8Array(a); // copy a into b
7b[0] = 10
8assert.equal(a[0], 1);
9assert.equal(b[0], 10);
10assert(b.length, 3);
11
12// From a length. 0 initialized
13c = new Int8Array(2);
14assert.equal(c.length, 2);
15for (let i of c) {
16 assert.equal(i, 0);
17}
18
19assert.equal(c.BYTES_PER_ELEMENT, 1);
./code/int32_array.js
1// https://nodejs.org/api/assert.html
2const assert = require('node:assert');
3
4// 8 bytes buffer
5let buffer = new ArrayBuffer(8);
6
7let a = new Int32Array(buffer);
8assert.equal(a.length, 2); // 2 ints
9assert.equal(a.byteLength, 8); // 8 bytes
10assert.equal(a.byteOffset, 0);
11
12// a and b share the same buffer
13let b = new Int8Array(buffer);
14
15a[0] = 0x12345678;
16assert.equal(b[0], 0x78); // little endian
17assert.equal(b[1], 0x56); // little endian
18assert.equal(b[2], 0x34); // little endian
19assert.equal(b[3], 0x12); // little endian
20
21// subarray(start, end) -> No copy
22let c = b.subarray(1, 3); // view to the same buffer, no copy
23assert.equal(c.length, 2);
24assert.equal(c.byteOffset, 1);
25b[1] = 0x23;
26b[2] = 0x32;
27assert.equal(c[0], 0x23);
28assert.equal(c[1], 0x32);
29
30// slice(start, end) -> copy
31let d = b.slice(1, 3); // copy
32assert.equal(d.length, 2);
33assert.equal(d[0], 0x23);
34assert.equal(d[1], 0x32);
35b[0] = 0x11;
36b[1] = 0x22;
37assert.equal(d[0], 0x23);
38assert.equal(d[1], 0x32);
39
40// share the same buffer
41let e = new Int8Array(b.buffer);
42b[0] = 0x30;
43assert.equal(e[0], 0x30);