What is typedarray-pool?
The 'typedarray-pool' npm package provides efficient memory management for typed arrays by reusing and recycling them. This can help reduce garbage collection overhead and improve performance in applications that frequently allocate and deallocate typed arrays.
What are typedarray-pool's main functionalities?
Allocate a Typed Array
This feature allows you to allocate a new typed array from the pool. The 'mallocFloat32' function allocates a Float32Array of the specified length (10 in this case).
const pool = require('typedarray-pool');
const array = pool.mallocFloat32(10);
Free a Typed Array
This feature allows you to return a typed array back to the pool for reuse. The 'free' function takes the typed array and makes it available for future allocations.
pool.free(array);
Reallocate a Typed Array
This feature allows you to allocate a new typed array with a different size. The 'mallocFloat32' function can be called again with a different length (20 in this case) to get a new array.
const newArray = pool.mallocFloat32(20);
Other packages similar to typedarray-pool
buffer-pool
The 'buffer-pool' package provides a similar functionality for managing and reusing Buffer objects. It helps in reducing memory fragmentation and garbage collection overhead by pooling Buffer instances. Unlike 'typedarray-pool', it is specifically designed for Node.js Buffer objects.
array-pool
The 'array-pool' package offers a general-purpose pool for JavaScript arrays. It allows for efficient reuse of arrays to minimize garbage collection. While 'typedarray-pool' focuses on typed arrays, 'array-pool' is more generic and can be used for any type of JavaScript array.
object-pool
The 'object-pool' package provides a pooling mechanism for JavaScript objects. It is useful for managing and reusing objects to reduce memory allocation overhead. Although it is not specific to typed arrays, it offers similar benefits in terms of memory management and performance optimization.
typedarray-pool
A global pool for typed arrays.
Example
var pool = require("typedarray-pool")
var f = pool.malloc(128, "float")
pool.free(f)
Install
npm install typedarray-pool
API
var pool = require("typedarray-pool")
pool.malloc(n[, dtype])
Allocates a typed array (or ArrayBuffer) with at least n elements.
Returns A typed array with at least n
elements in it.
Note You can avoid the dispatch by directly calling one of the following methods:
pool.mallocUint8
pool.mallocUint16
pool.mallocUint32
pool.mallocInt8
pool.mallocInt16
pool.mallocInt32
pool.mallocFloat
pool.mallocDouble
pool.mallocArrayBuffer
pool.free(array)
Returns the array back to the pool.
array
The array object to return to the pool.
Note You can speed up the method if you know the type of array before hand by calling one of the following:
pool.freeUint8
pool.freeUint16
pool.freeUint32
pool.freeInt8
pool.freeInt16
pool.freeInt32
pool.freeFloat
pool.freeDouble
pool.freeArrayBuffer
pool.clearCache()
Removes all references to cached arrays. Use this when you are done with the pool to return all the cached memory to the garbage collector.
FAQ
Why cache typed arrays?
Creating typed arrays is stupidly expensive in most JS engines. So it makes sense to pool them, both so that frequently used typed arrays stay hot in cache and so that you can avoid having to trigger some expensive realloc operation whenever you use them.
Why not cache ArrayBuffers instead?
Because creating a typed array from an array buffer is almost as expensive as allocating the typed array in the first place. While this approach would save memory, it doesn't give much of a performance benefit for small arrays. (See for example this experiment:
https://github.com/mikolalysenko/typedarray-cache-experiment
Is this library safe to use?
Only if you know what you are doing. This library will create a global pool of typed array buffers that you can use across many modules. The downside though is that you have to manage all the memory yourself, so you can easily shoot yourself in the foot if you screw up.
Credits
(c) 2013 Mikola Lysenko. MIT License