Example program. To use an efficient byte array, we first create an ArrayBuffer. We specify the exact number of bytes (not elements) we want in the constructor.
Then We create a "view" upon the ArrayBuffer. Here we use an Int32Array to have 4-byte elements in our array.
Info We can assign elements to the Int32Array and read them back. In this way it is like any other array.
// Create a buffer of 40 bytes.
var buffer = new ArrayBuffer(4 * 10);
// Use Int32Array on this buffer.
var array = new Int32Array(buffer);
// Assign elements.
for (var i = 0; i < array.length; i++) {
array[i] = i;
}
array[0] = 300;
// Read from Int32Array.
for (var i = 0; i < array.length; i++) {
console.log("INT32ARRAY ELEMENT: " + array[i]);
}INT32ARRAY ELEMENT: 300
INT32ARRAY ELEMENT: 1
INT32ARRAY ELEMENT: 2
INT32ARRAY ELEMENT: 3
INT32ARRAY ELEMENT: 4
INT32ARRAY ELEMENT: 5
INT32ARRAY ELEMENT: 6
INT32ARRAY ELEMENT: 7
INT32ARRAY ELEMENT: 8
INT32ARRAY ELEMENT: 9
Array literals. A typed array can be initialized from an array literal. We pass the array directly to the Int32Array constructor. Other constructors like Int8Array also work.
Tip For performance, it is best to test your program before using Int32Array around array literals. There is a conversion cost here.
// Use array literal to create Int32Array.
var values = new Int32Array([10, 8000000, 8]);
for (var i = 0; i < values.length; i++) {
console.log("INT32ARRAY ELEMENT: " + values[i]);
}
// Use array literal to create Int8Array.
var valuesSmall = new Int8Array([0, 20, 1, 0]);
for (var i = 0; i < valuesSmall.length; i++) {
console.log("INT8ARRAY ELEMENT: " + valuesSmall[i]);
}INT32ARRAY ELEMENT: 10
INT32ARRAY ELEMENT: 8000000
INT32ARRAY ELEMENT: 8
INT8ARRAY ELEMENT: 0
INT8ARRAY ELEMENT: 20
INT8ARRAY ELEMENT: 1
INT8ARRAY ELEMENT: 0
Memory usage. Suppose we want to manipulate 10 million integers. With an Int32Array we save memory over an Array. I tested 2 programs with large arrays.
Info The ArrayBuffer has 40 million bytes, and the Array has 10 million objects.
Result The Int32 array program (1) uses about 7 MB less RAM when measured in Process Explorer on Windows.
58.3 MB ArrayBuffer, Int32Array
65.3 MB Array
Some notes. Many constructs in Node do not need low-level structures. But for an array with millions of elements, a typed array can make a program use much less memory.
And With improved locality of reference, it will execute much faster. Each element is more compact.
Review. With typed arrays, Node.js can support complex mathematical or numerical applications. Many numbers can be used in a program.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 13, 2023 (edit).