p-j-s
v1.0.0-beta
Published
A library to parallelize `map`, `filter` and `reduce` operations on typed arrays through the use of Web Workers.
Downloads
18
Readme
p-j-s
A library to parallelize map
, filter
and reduce
operations on typed arrays through the use of Web Workers.
Installing
npm i p-j-s
Usage
It's as simple as:
var pjs = require('p-j-s');
pjs.init(); // initialize the library
pjs(new Uint32Array([1,2,3,4]))
.filter(function(e){
return e % 2 === 0;
}).map(function(e){
return e * 2;
}).seq(function(err, result){
// result is [4,8] a new Uint32Array
// if we are not using the library any more cleanup once we are done
pjs.terminate();
});
Operations
map
The map
operation invokes the mapper
function on each element of the wrapped TypedArray
. It produces a new array of the same type where each element is the result of the mapper
function.
filter
The filter
operation invokes the predicate
function on each element of the wrapped TypedArray
. It produces a new array of the same type which only includes the original elements for which the predicate
function returns true
(or a truthy value).
reduce
The reduce
operation invokes the reducer
function on each element of the wrapped TypedArray
passing the value of the previous invocation as current
.
The reduction is first performed in the Web Workers using identity
as the intial value for current
. The results from the Web Workers are collected and a new reduction is performed on them using seed
and identityReducer
function.
Documentation
You can find out more by checking out the complete API and the How Tos in our wiki.
Acknowledgements
- Using this great seed project from @mgonto.
- @mraleph for IR Hydra and the help he provided to use it.