0.2.1 • Public • Published


    npm npm GitHub issues

    A light helper for parallel calculations on one-dimensional arrays. Web workers are alternative here for method.

    Demo and comparsion

    You can see a live demo at The source code of this demo is available in the repository.

    Example of usage: Griffeath's machine.


    Run in a console:

    npm i parallel1d

    Web worker needs an external file as a browsers limitation. Web worker always gets data property in onmessage function. Parallel1d will also add from and to properties to divide work.

     * sample.worker.js
    // just one calculation function for example
    function double (n) {
      return n * 2
    // it gets passed options for worker, `from` and `to`
    onmessage = function ({ data: { input, from, to } }) {
      const result = []
      for (let j = from; j < to; j++) {
        const n = input[j]

    A worker returns the resulting array through the postMessage function.

    The worker may be imported just by const myWorker = new Worker('sample.worker.js'), or with worker-loader in case of using Webpack.

     * index.js
    import Parallel from 'parallel1d'
    import SampleWorker from 'worker-loader!./sample.worker.js'
    const someNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    // pass worker constructor and callback to process resulting array
    const workers = new Parallel(SampleWorker, console.log)
    // pass any options for worker and length of array to divide it to workers
    workers.start({ input: someNumbers }, someNumbers.length)
    // console: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

    That's it.


    Besides worker constructor and callback to process resulting array parallel1d constructor accepts options:

    const workers = new Parallel(SampleWorker, console.log, {
      // handler for errors, console by default
      handleError: console.error,
      // how much workers will be spawned, number of logical processors by default
      numberOfWorkers: navigator.hardwareConcurrency,
      // type of array to be returned from parallel1d and workers
      // may be typed array like Int32Array and Uint8ClampedArray, usual array by default
      ArrayConstructor: Array,
    // get numberOfWorkers

    Get the numberOfWorkers set in options or by default from a threads property.


    If you need to stop all workers immediately, call:


    Just 419 bytes

    Size and times are defined with size-limit:

      Package size: 419 B with all dependencies, minified and gzipped
      Loading time: 10 ms on slow 3G
      Running time: 45 ms on Snapdragon 410
      Total time:   55 ms

    How to run demo locally

    Run in a console:

    git clone
    cd parallel1d
    yarn run start

    For sure, you can use npm instead of yarn.


    npm i parallel1d

    DownloadsWeekly Downloads






    Unpacked Size

    17.3 kB

    Total Files


    Last publish


    • avatar