@stdlib/array-pool

Typed array pool.

https://github.com/stdlib-js/array-pool

Science Score: 44.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (16.0%) to scientific vocabulary

Keywords

array data float32array float64array int16array int32array javascript matrix ndarray node node-js nodejs stdlib structure typed typed-array types uint32array vector
Last synced: 4 months ago · JSON representation ·

Repository

Typed array pool.

Basic Info
Statistics
  • Stars: 5
  • Watchers: 3
  • Forks: 1
  • Open Issues: 0
  • Releases: 0
Topics
array data float32array float64array int16array int32array javascript matrix ndarray node node-js nodejs stdlib structure typed typed-array types uint32array vector
Created over 4 years ago · Last pushed 4 months ago
Metadata Files
Readme Changelog Contributing License Code of conduct Citation Security

README.md

About stdlib...

We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.

The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.

When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.

To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!

typedarraypool

NPM version Build Status Coverage Status <!-- dependencies -->

Allocate typed arrays from a typed array memory pool.

## Installation ```bash npm install @stdlib/array-pool ``` Alternatively, - To load the package in a website via a `script` tag without installation and bundlers, use the [ES Module][es-module] available on the [`esm`][esm-url] branch (see [README][esm-readme]). - If you are using Deno, visit the [`deno`][deno-url] branch (see [README][deno-readme] for usage intructions). - For use in Observable, or in browser/node environments, use the [Universal Module Definition (UMD)][umd] build available on the [`umd`][umd-url] branch (see [README][umd-readme]). The [branches.md][branches-url] file summarizes the available branches and displays a diagram illustrating their relationships. To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.
## Usage ```javascript var typedarraypool = require( '@stdlib/array-pool' ); ``` #### typedarraypool( \[dtype] ) Returns an **uninitialized** [typed array][mdn-typed-array] having a specified [data type][@stdlib/array/typed-dtypes] `dtype`. ```javascript var arr = typedarraypool(); // returns [] // ... typedarraypool.free( arr ); ``` By default, the output [typed array][mdn-typed-array] is `float64`. To specify an alternative [data type][@stdlib/array/typed-dtypes], set the `dtype` parameter. ```javascript var arr = typedarraypool( 'int32' ); // returns [] // ... typedarraypool.free( arr ); ``` #### typedarraypool( length\[, dtype] ) Returns an **uninitialized** [typed array][mdn-typed-array] having a specified `length` from a [typed array][mdn-typed-array] memory pool. ```javascript var arr1 = typedarraypool( 5 ); // returns var arr2 = typedarraypool( 5, 'uint8' ); // returns // ... typedarraypool.free( arr1 ); typedarraypool.free( arr2 ); ``` #### typedarraypool( typedarray\[, dtype] ) Returns a pooled [typed array][mdn-typed-array] from another [typed array][mdn-typed-array]. ```javascript var arr1 = typedarraypool( [ 5.0, -3.0, 2.0 ] ); // returns [ 5.0, -3.0, 2.0 ] var arr2 = typedarraypool( arr1 ); // returns [ 5.0, -3.0, 2.0 ] var arr3 = typedarraypool( arr1, 'int32' ); // returns [ 5, -3, 2 ] // ... typedarraypool.free( arr1 ); typedarraypool.free( arr2 ); typedarraypool.free( arr3 ); ``` #### typedarraypool( obj\[, dtype] ) Returns a pooled [typed array][mdn-typed-array] from an array-like `object`. ```javascript var arr1 = typedarraypool( [ 0.5, 0.5, 0.5 ] ); // returns [ 0.5, 0.5, 0.5 ] var arr2 = typedarraypool( [ 0.5, 0.5, 0.5 ], 'float32' ); // returns [ 0.5, 0.5, 0.5 ] // ... typedarraypool.free( arr1 ); typedarraypool.free( arr2 ); ``` #### typedarraypool.malloc( \[dtype] ) Returns an **uninitialized** [typed array][mdn-typed-array] having a specified [data type][@stdlib/array/typed-dtypes] `dtype`. ```javascript var arr1 = typedarraypool.malloc(); // returns [] var arr2 = typedarraypool.malloc( 'int32' ); // returns [] // ... typedarraypool.free( arr1 ); typedarraypool.free( arr2 ); ``` #### typedarraypool.malloc( length\[, dtype] ) Returns an **uninitialized** [typed array][mdn-typed-array] having a specified `length` from a [typed array][mdn-typed-array] memory pool. ```javascript var arr1 = typedarraypool.malloc( 5 ); // returns var arr2 = typedarraypool.malloc( 5, 'uint8' ); // returns // ... typedarraypool.free( arr1 ); typedarraypool.free( arr2 ); ``` #### typedarraypool.malloc( typedarray\[, dtype] ) Returns a pooled [typed array][mdn-typed-array] from another [typed array][mdn-typed-array]. ```javascript var arr1 = typedarraypool.malloc( [ 5.0, -3.0, 2.0 ] ); // returns [ 5.0, -3.0, 2.0 ] var arr2 = typedarraypool.malloc( arr1 ); // returns [ 5.0, -3.0, 2.0 ] var arr3 = typedarraypool.malloc( arr1, 'int32' ); // returns [ 5, -3, 2 ] // ... typedarraypool.free( arr1 ); typedarraypool.free( arr2 ); typedarraypool.free( arr3 ); ``` #### typedarraypool.malloc( obj\[, dtype] ) Returns a pooled [typed array][mdn-typed-array] from an array-like `object`. ```javascript var arr1 = typedarraypool.malloc( [ 0.5, 0.5, 0.5 ] ); // returns [ 0.5, 0.5, 0.5 ] var arr2 = typedarraypool.malloc( [ 0.5, 0.5, 0.5 ], 'float32' ); // returns [ 0.5, 0.5, 0.5 ] // ... typedarraypool.free( arr1 ); typedarraypool.free( arr2 ); ``` #### typedarraypool.calloc( \[dtype] ) Returns a **zero-initialized** [typed array][mdn-typed-array] having a specified [data type][@stdlib/array/typed-dtypes] `dtype`. ```javascript var arr1 = typedarraypool.calloc(); // returns [] var arr2 = typedarraypool.calloc( 'int32' ); // returns [] // ... typedarraypool.free( arr1 ); typedarraypool.free( arr2 ); ``` #### typedarraypool.calloc( length\[, dtype] ) Returns a **zero-initialized** [typed array][mdn-typed-array] having a specified `length` from a [typed array][mdn-typed-array] memory pool. ```javascript var arr1 = typedarraypool.calloc( 5 ); // returns [ 0.0, 0.0, 0.0, 0.0, 0.0 ] var arr2 = typedarraypool.calloc( 5, 'uint8' ); // returns [ 0, 0, 0, 0, 0 ] // ... typedarraypool.free( arr1 ); typedarraypool.free( arr2 ); ``` #### typedarraypool.free( buf ) Frees a [typed array][mdn-typed-array] or typed array [buffer][mdn-arraybuffer] for use in a future allocation. ```javascript var arr = typedarraypool( 10, 'float64' ); // returns // ... // Free the allocated typed array for use in a future allocation: typedarraypool.free( arr ); // Create another typed array: arr = typedarraypool( 10, 'float64' ); // returns // ... // Free the allocated typed array buffer for use in a future allocation: typedarraypool.free( arr.buffer ); ``` #### typedarraypool.clear() Clears the [typed array][mdn-typed-array] pool allowing garbage collection of previously allocated (and currently free) [array buffers][mdn-arraybuffer]. ```javascript var arr = typedarraypool( 10, 'float64' ); // returns // ... typedarraypool.free( arr ); // ... // Clear all freed buffers: typedarraypool.clear(); ``` #### typedarraypool.highWaterMark **Read-only** property returning the pool's high water mark (in bytes). ```javascript var limit = typedarraypool.highWaterMark; // returns ``` Once a high water mark is reached, [typed array][mdn-typed-array] allocation **fails**. #### typedarraypool.nbytes **Read-only** property returning the total number of allocated bytes. ```javascript var arr = typedarraypool( 5, 'float64' ); var nbytes = typedarraypool.nbytes; // returns ``` The returned value is the total **accumulated** value. Hence, anytime a pool must allocate a new [array buffer][mdn-arraybuffer] (i.e., more memory), the pool increments this value. The only time this value is decremented is when a pool is cleared. This behavior means that, while allocated buffers which are never freed may, in fact, be garbage collected, they continue to count against the high water mark limit. Accordingly, you should **always** free allocated buffers in order to prevent the pool from believing that non-freed buffers are continually in use. #### typedarraypool.factory( \[options] ) Creates a new [typed array][mdn-typed-array] pool. ```javascript var pool = typedarraypool.factory(); var arr = pool( 5, 'float64' ); // returns // ... pool.free( arr ); ``` The method accepts the following `options`: - **highWaterMark**: maximum total memory (in bytes) which can be allocated. Default: `2^53` bytes. By default, the maximum total memory a pool may allocate is `2^53` bytes (approximately `1` petabyte, which, in practical terms, means a pool has **unlimited** capacity). To specify an alternative limit, set the `highWaterMark` option. ```javascript // Create a new typed array pool which can allocate up to 1MB: var pool = typedarraypool.factory({ 'highWaterMark': 1e6 }); var arr = pool( 5, 'float64' ); // returns // ... pool.free( arr ); ```
## Notes - Uninitialized typed arrays may contain sensitive contents. If security is paramount (e.g., if freed [typed arrays][mdn-typed-array] have been used to store sensitive contents), use `calloc`. - An allocated [typed array][mdn-typed-array] is **guaranteed** to have an underlying [array buffer][mdn-arraybuffer] with _at least_ `N * w` bytes, where `N` is the number of [typed array][mdn-typed-array] elements and `w` is the number of bytes per element. Note, however, that the underlying [array buffer][mdn-arraybuffer] is likely to have **excess** capacity. Thus, if you create many [typed arrays][mdn-typed-array] which are held in memory and are **not** freed, you are likely to consume significantly more memory than if you had directly used [typed array][mdn-typed-array] constructors. However, if you create many [typed arrays][mdn-typed-array] which are rapidly discarded and of relatively large size, then using a [typed array][mdn-typed-array] pool can offer significant performance advantages.
## Examples ```javascript var randu = require( '@stdlib/random-base-randu' ); var typedarraypool = require( '@stdlib/array-pool' ); // Create a typed array pool which can allocate at most 1GB: var typedarray = typedarraypool.factory({ 'highWaterMark': 1e9 }); // Inspect the pool: console.log( 'Max bytes: %d', typedarray.highWaterMark ); console.log( 'nbytes: %d', typedarray.nbytes ); // Allocate an array for storing double-precision floating-point numbers: var arr1 = typedarray( 5, 'float64' ); // returns [ 0.0, 0.0, 0.0, 0.0, 0.0 ] // Inspect the pool: console.log( 'nbytes: %d', typedarray.nbytes ); // Fill the array... var i; for ( i = 0; i < arr1.length; i++ ) { arr1[ i ] = randu(); } // Inspect array contents: console.log( arr1 ); // Free the array: typedarray.free( arr1 ); // Allocate another array similar to the previous one: var arr2 = typedarray( 5, 'float64' ); // returns // Check that we have been returned a new typed array view: console.log( arr2 === arr1 ); // => false // Inspect array contents: console.log( arr2 ); // Free the array: typedarray.free( arr2 ); // Allocate an initialized array: var arr3 = typedarray.calloc( 5, 'float64' ); // returns [ 0.0, 0.0, 0.0, 0.0, 0.0 ] // Inspect array contents: console.log( arr3 ); // Free the array: typedarray.free( arr3 ); // Clear the pool: typedarray.clear(); // Inspect the pool: console.log( 'nbytes: %d', typedarray.nbytes ); ```
* * * ## Notice This package is part of [stdlib][stdlib], a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more. For more information on the project, filing bug reports and feature requests, and guidance on how to develop [stdlib][stdlib], see the main project [repository][stdlib]. #### Community [![Chat][chat-image]][chat-url] --- ## License See [LICENSE][stdlib-license]. ## Copyright Copyright © 2016-2025. The Stdlib [Authors][stdlib-authors].

Owner

  • Name: stdlib
  • Login: stdlib-js
  • Kind: organization

Standard library for JavaScript.

Citation (CITATION.cff)

cff-version: 1.2.0
title: stdlib
message: >-
  If you use this software, please cite it using the
  metadata from this file.

type: software

authors:
  - name: The Stdlib Authors
    url: https://github.com/stdlib-js/stdlib/graphs/contributors

repository-code: https://github.com/stdlib-js/stdlib
url: https://stdlib.io

abstract: |
  Standard library for JavaScript and Node.js.

keywords:
  - JavaScript
  - Node.js
  - TypeScript
  - standard library
  - scientific computing
  - numerical computing
  - statistical computing

license: Apache-2.0 AND BSL-1.0

date-released: 2016

GitHub Events

Total
  • Push event: 14
Last Year
  • Push event: 14

Committers

Last synced: almost 2 years ago

All Time
  • Total Commits: 49
  • Total Committers: 1
  • Avg Commits per committer: 49.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 13
  • Committers: 1
  • Avg Commits per committer: 13.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
stdlib-bot n****y@s****o 49
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 4 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • npm 6 last-month
  • Total dependent packages: 1
  • Total dependent repositories: 0
  • Total versions: 9
  • Total maintainers: 4
npmjs.org: @stdlib/array-pool

Typed array pool.

  • Homepage: https://stdlib.io
  • License: Apache-2.0
  • Latest release: 0.2.1
    published almost 2 years ago
  • Versions: 9
  • Dependent Packages: 1
  • Dependent Repositories: 0
  • Downloads: 6 Last month
Rankings
Downloads: 12.9%
Stargazers count: 13.9%
Dependent packages count: 16.2%
Average: 17.1%
Forks count: 17.4%
Dependent repos count: 25.3%
Funding
  • type: opencollective
  • url: https://opencollective.com/stdlib
Last synced: 4 months ago

Dependencies

package.json npm
  • @stdlib/array-complex128 ^0.0.x development
  • @stdlib/array-complex64 ^0.0.x development
  • @stdlib/array-float32 ^0.0.x development
  • @stdlib/array-float64 ^0.0.x development
  • @stdlib/array-int16 ^0.0.x development
  • @stdlib/array-int32 ^0.0.x development
  • @stdlib/array-int8 ^0.0.x development
  • @stdlib/array-uint16 ^0.0.x development
  • @stdlib/array-uint32 ^0.0.x development
  • @stdlib/array-uint8 ^0.0.x development
  • @stdlib/array-uint8c ^0.0.x development
  • @stdlib/assert-instance-of ^0.0.x development
  • @stdlib/assert-is-complex-typed-array ^0.0.x development
  • @stdlib/assert-is-function ^0.0.x development
  • @stdlib/assert-is-typed-array ^0.0.x development
  • @stdlib/bench ^0.0.x development
  • @stdlib/complex-float32 ^0.0.x development
  • @stdlib/complex-float64 ^0.0.x development
  • @stdlib/complex-imag ^0.0.x development
  • @stdlib/complex-imagf ^0.0.x development
  • @stdlib/complex-real ^0.0.x development
  • @stdlib/complex-realf ^0.0.x development
  • @stdlib/math-base-special-pow ^0.0.x development
  • @stdlib/random-base-randu ^0.0.x development
  • istanbul ^0.4.1 development
  • tap-spec 5.x.x development
  • tape git+https://github.com/kgryte/tape.git#fix/globby development
  • @stdlib/array-base-arraylike2object ^0.0.x
  • @stdlib/array-buffer ^0.0.x
  • @stdlib/array-typed-ctors ^0.0.x
  • @stdlib/assert-has-own-property ^0.0.x
  • @stdlib/assert-is-arraybuffer ^0.0.x
  • @stdlib/assert-is-collection ^0.0.x
  • @stdlib/assert-is-complex128array ^0.0.x
  • @stdlib/assert-is-complex64array ^0.0.x
  • @stdlib/assert-is-nonnegative-integer ^0.0.x
  • @stdlib/assert-is-plain-object ^0.0.x
  • @stdlib/assert-is-string ^0.0.x
  • @stdlib/assert-is-typed-array-like ^0.0.x
  • @stdlib/math-base-special-ceil ^0.0.x
  • @stdlib/math-base-special-ceil2 ^0.0.x
  • @stdlib/math-base-special-floor ^0.0.x
  • @stdlib/math-base-special-log2 ^0.0.x
  • @stdlib/math-base-special-min ^0.0.x
  • @stdlib/strided-base-reinterpret-complex128 ^0.0.x
  • @stdlib/strided-base-reinterpret-complex64 ^0.0.x
  • @stdlib/string-format ^0.0.x
  • @stdlib/types ^0.0.x
  • @stdlib/utils-copy ^0.0.x
  • @stdlib/utils-define-nonenumerable-read-only-accessor ^0.0.x
  • @stdlib/utils-define-nonenumerable-read-only-property ^0.0.x
.github/workflows/benchmark.yml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
.github/workflows/cancel.yml actions
  • styfle/cancel-workflow-action 0.11.0 composite
.github/workflows/close_pull_requests.yml actions
  • superbrothers/close-pull-request v3 composite
.github/workflows/examples.yml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
.github/workflows/npm_downloads.yml actions
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • actions/upload-artifact v3 composite
  • distributhor/workflow-webhook v3 composite
.github/workflows/productionize.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • stdlib-js/bundle-action main composite
  • stdlib-js/transform-errors-action main composite
.github/workflows/publish.yml actions
  • JS-DevTools/npm-publish v1 composite
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • styfle/cancel-workflow-action 0.11.0 composite
.github/workflows/test.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
.github/workflows/test_bundles.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • denoland/setup-deno v1 composite
.github/workflows/test_coverage.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite
  • codecov/codecov-action v3 composite
  • distributhor/workflow-webhook v3 composite
.github/workflows/test_install.yml actions
  • act10ns/slack v1 composite
  • actions/checkout v3 composite
  • actions/setup-node v3 composite