typedarray-pool
Advanced tools
Comparing version 0.0.0 to 0.0.1
{ | ||
"name": "typedarray-pool", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"description": "Reuse typed arrays", | ||
@@ -5,0 +5,0 @@ "main": "pool.js", |
18
pool.js
@@ -165,2 +165,18 @@ "use strict" | ||
} | ||
exports.malloc = malloc | ||
exports.malloc = malloc | ||
function clearCache() { | ||
for(var i=0; i<32; ++i) { | ||
UINT8[i].length = 0 | ||
UINT16[i].length = 0 | ||
UINT32[i].length = 0 | ||
INT8[i].length = 0 | ||
INT16[i].length = 0 | ||
INT32[i].length = 0 | ||
FLOAT[i].length = 0 | ||
DOUBLE[i].length = 0 | ||
DATA[i].length = 0 | ||
} | ||
} | ||
exports.clearCache = clearCache |
@@ -53,6 +53,11 @@ typedarray-pool | ||
## Why cache typed arrays? | ||
### `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? | ||
### 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: | ||
@@ -62,3 +67,3 @@ | ||
## Is this library safe to use? | ||
### 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. | ||
@@ -65,0 +70,0 @@ |
@@ -7,2 +7,4 @@ "use strict" | ||
pool.clearCache() | ||
for(var i=1; i<100; ++i) { | ||
@@ -55,4 +57,6 @@ var a | ||
} | ||
pool.clearCache() | ||
t.end() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8289
212
71