typed-morph
High order functions (map, reduce, filter) to work with Javascript typed arrays. They don't make use of intermediate arrays for chaining, and instead takes advantage of the iterator protocol.
Installing
npm i typed-morph
Usage
map
var wrap = ;var elements = 123;var mapped = ; // mapped now is a Uint16Array with [2,4,6] content
Alternatively, you can avoid invoking col
and use the iterator:
var wrap = ;var elements = 123;// returns an object that matches the JS iterator protocol https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocolvar iterator = ; console; // { done: false, value: 2 }
filter
var wrap = ;var elements = 1234;var filtered = ; // mapped now is a Uint16Array with [2,4] content
Alternatively, you can avoid invoking col
and use the iterator:
var wrap = ;var elements = 1234;// returns an object that matches the JS iterator protocol https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocolvar filtered = ; console; // { done: false, value: 2 }
reduce
var elements = 1234; var sum = ; console; // 10
Chaining
Sometimes you need to perform multiple sequential operations on the same array. The typed-morpth
implementation does not create any intermediate arrays until either col
or reduce
are invoked.
For example:
var elements = 14710;var iter = ; // at this point no processing has taken placeiter; to;
Acknowledgements
- Using this great seed project from @mgonto.