npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

compute-typed-array-function

v1.0.3

Published

Applies a function to each typed array element.

Downloads

27

Readme

Typed Array Function

NPM version Build Status Coverage Status Dependencies

Applies a function to each typed array element.

Installation

$ npm install compute-typed-array-function

For use in the browser, use browserify.

Usage

var arrayfun = require( 'compute-typed-array-function' );

arrayfun( fcn, ...array[, options] )

Applies a function to each typed array element.

var arr = new Int8Array( [1,2,3,4,5] );

function add5( val ) {
	return val + 5;
}

var out = arrayfun( add5, arr );
// returns Float64Array( [6,7,8,9,10] )

The function accepts the following options:

  • dtype: output data type. Default: float64.
  • out: boolean indicating whether an output typed array has been provided. Default: false.

By default, the output typed array data type is float64 in order to preserve precision. To specify a different data type, set the dtype option (see compute-array-constructors for a list of acceptable data types).

var out = arrayfun( add5, arr, {
	'dtype': 'int8';
});
// return Int8Array( [6,7,8,9,10] )

By default, the function returns a new typed array. To mutate a typed array (e.g., when input values can be discarded or when optimizing memory usage), set the out option to true to indicate that an output typed array has been provided as the first typed array argument.

var out = Uint8Array( 5 );

arrayfun( add5, out, arr, {
	'out': 'true';
});
// returns Uint8Array( [6,7,8,9,10] )

// Works with generic arrays, as well...
out = [ 0, 0, 0, 0, 0 ];

arrayfun( add5, out, arr, {
	'out': 'true';
});
// returns [ 6, 7, 8, 9, 10 ]

===

Factory

The main exported function does not make any assumptions regarding the number of input typed arrays. To create a reusable typed array function where the number of input typed arrays is known, a factory method is provided.

arrayfun.factory( [fcn,] num[, options] )

Creates an apply function to apply a function to each typed array element.

var afun = arrayfun.factory( 2 );

function add( x, y ) {
	return x + y;
}

var arr1 = new Int16Array( 5 ),
	arr2 = new Uint32Array( 5 );

for ( var i = 0; i < 5; i++ ) {
	arr1[ i ] = 5;
	arr2[ i ] = i + 5;
}
// arr1 = Int16Array( [5,5,5,5,5] )
// arr2 = Uint32Array( [5,6,7,8,9] )

var out = afun( add, arr1, arr2 );
// returns Float64Array( [10,11,12,13,14] )

An apply function may be provided during function creation.

var aadd = arrayfun.factory( add, 2 );

var out = aadd( arr1, arr2 );
// returns Float64Array( [10,11,12,13,14] )

The function accepts the following options:

  • dtype: output data type. Default: float64.

By default, the output typed array data type is float64. To specify a different data type, set the dtype option.

var aadd = arrayfun.factory( add, 2, {
	'dtype': 'int32';
});

var out = aadd( arr1, arr2 );
// returns Int32Array( [10,11,12,13,14] )

// ...and for all subsequent calls...
out = aadd( arr1, arr2 );
// returns Int32Array( [10,11,12,13,14] )

Note: a factory function always returns a new typed array.

===

Create

To facilitate using typed array functions within an application where input arguments are of known types and where memory management occurs externally, a method to create minimal typed array functions is provided.

arrayfun.create( [fcn,] num )

Creates an apply function to apply a function to each typed array element, where num is the number of input typed arrays excluding the output typed array.

var afcn = arrayfun.create( 2 ),
	out = new Array( 5 );

out = afcn( add, out, arr1, arr2 );
// returns [ 10, 11, 12, 13, 14 ]

function subtract( x, y ) {
	return x - y;
}

out = afcn( subtract, out, arr1, arr2 );
// returns [ 0, -1, -2, -3, -4 ]

An apply function may be provided during function creation.

var aadd = arrayfun.create( add, 2 );

var out = aadd( out, arr1, arr2 );
// returns [ 10, 11, 12, 13, 14 ]

===

Raw

Lower-level APIs are provided which forgo some of the guarantees of the above APIs, such as input argument validation. While use of the above APIs is encouraged in REPL environments, use of the lower-level interfaces may be warranted when arguments are of a known type or when performance is paramount.

arrayfun.raw( fcn, ...array[, options] )

Applies a function to each typed array element.

var arr = new Float32Array( 5 );

var out = arrayfun.raw( add5, arr );
// returns Float64Array( [5,5,5,5,5] )

The function accepts the same options as the main exported function.

arrayfun.rawFactory( [fcn,] num[, options] )

Creates an apply function to apply a function to each typed array element.

var afun = arrayfun.rawFactory( 2 );

var out = afun( add, arr1, arr2 );
// returns Float64Array( [10,11,12,13,14] )

The function accepts the same options as arrayfun.factory().

Notes

  • Both factory methods, as well as the .create() method, use dynamic code evaluation. Beware when using these methods in the browser as they may violate your content security policy (CSP).

Examples

var arrayfun = require( 'compute-typed-array-function' );

var arr1,
	arr2,
	out,
	i;

arr1 = new Float32Array( 25 );
for ( i = 0; i < arr1.length; i++ ) {
	arr1[ i ] = i;
}

arr2 = new Uint8Array( 25 );
for ( i = 0; i < arr2.length; i++ ) {
	arr2[ i ] = 5;
}

function add( x, y ) {
	return x + y;
}

out = arrayfun( add, arr1, arr2 );
console.log( out );

To run the example code from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.

Copyright

Copyright © 2015. The Compute.io Authors.