A simple Javascript library for handling Workers.
npm install multicoreMulticore
=========================
A simple Javascript library for handling Web Workers.
Currently being developed. Only works on the web.
Progress so far:
- Faster for arithmetic operations than ParallelJS.
Test: Operating on a list of 100k elements, multiplying and reducing them to one value.
Result: Multicore: ~200ms - ParallelJS ~20s
- Now supports TypedArrays (and any kind of transferable) for futher increase in speed and memory use.
``
Use yarn or npm:
yarn add multicore --save
npm install --save multicore
`
This assumes that you’re using npm package manager with a module bundler like Webpack or Browserify to consume CommonJS modules.
##### TLDR;
- Use .data or, if you know size/type of array, a TypedArray function like .uInt8.
- Then manipulate data by chaining API-methods: Multicore.data([1,2,3]).reduce((acc,val) => acc+val); [...].reduce().map().then(result => { doSomething(result); })
- Retrieve the data from the instance promise:
---
#### Import Multicore
Using either:
``
const Multicore = require('multicore').default;
import Multicore from 'multicore';
#### Create an instance
Start an instance for data you wish you manipulate (can either be used as a constructor or with these static shorthand-methods):
##### Constructor
Create an instance with constructor and save it to a variable. The Constructor can also be subclassed as per ES6 classes.
``
const data = new Multicore([1,2,3]); // Constructor
##### Static converter-functions
Use the generic .data method to get the same behavior as with constructor.
Use any of the TypedArray functions to pass in an array and use parallelized conversion to convert it to a typed array internally, and thereby being able to use buffers internally. Recommended for large arrays containing integers or floats, or anytime you know your array will only contain numbers.
TypedArray functions for the different types: .int8, .int16, .int32, .uInt8, .uInt16, .uInt32, .float32, .float16.
``
const data = Multicore.data([1,2,3]); // Use for generic data.
const data = Multicore.uInt8([1,2,3); // Will convert array to typed-array and be able to utilize buffers internally
#### Start manipulating
`The result is ${result}
// Manipulate saved instance
data.map(val => val*2)
.map(val => val*3)
.reduce((acc, val) => {
return acc + val;
})
.then(result => {
console.log();
});
// The result is 37
// Direct manipulation and conversion
Multicore.uInt8([1,2,3])
.map(val => val*2)
.then(console.log);
// [2,4,6]
`
`
const data = new Multicore(['a','b','c']);
data.spawn(data => {
return data.join(',').toUpperCase();
});
// "A,B,C"
`
`
const data = new Multicore([1,2,3]);
data.map(val => val*2);
// [2,4,6]
`
`
const data = new Multicore([1,2,3]);
data.reduce((acc, val) => {
return acc + val;
});
// 6
`
`
const data = new Multicore([1,2,3]);
data.foldr((acc, val) => {
return acc - val;
}, 0);
// -4
`
`
const data = new Multicore([1,2,3]);
data.filter((val) => {
return val > 2;
});
// 3
``
More methods under development.
- Adding full test-coverage.
- Switching to babel-implementation and building minified-js-files for CDN.
- Adding support for new operators: .parallel and .merge.
MIT