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 🙏

© 2024 – Pkg Stats / Ryan Hefner

parallel1d

v2.0.0

Published

Parallel computations on 1D arrays for Javascript

Downloads

4

Readme

Parallel 1D

npm npm GitHub issues

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

Demo and Comparsion

You can see a live demo at https://bouvens.github.io/parallel1d/ The source code of this demo is available in the repository.

Example of usage: Griffeath's Machine .

Usage

Run in a project root to install the package

npm i parallel1d

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

/**
 * mock.worker.js
 */

// just one calculation function for example
function double(n) {
  return n * 2
}

// it gets passed options for worker (i.e. `input`), and special props `from` and `to`
onmessage = function ({ data: { input, from, to } }) {
  const result = []

  for (let i = from; i < to; i++) {
    const n = input[i]

    result.push(double(n))
  }

  postMessage(result)
}

A worker should return the resulting array through the postMessage function.

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

/**
 * index.js
 */

import SampleWorker from 'worker-loader!./mock.worker.js'

You can run it as promise if we don't need to run the same workers with different data

import parallel from 'parallel1d/promisified'

const someNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

async function runWorkers() {
  const result = await parallel(SampleWorker, { input: someNumbers }, someNumbers.length)
  // it's possible to pass options as the 4th argument, check the Options section below
}

Or you can use the older approach

import Parallel from 'parallel1d'

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)

// You will see in console as result: [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

Options

We can pass options as the 4th argument to a promisified version

const options = {
  // handler for errors, console by default
  onError: console.error,
  // how much workers will be spawned, number of logical processors by default or 4 if undefined
  numberOfWorkers: navigator.hardwareConcurrency || 4,
  // type of array to be returned from parallel1d and workers
  // may be typed array like Int32Array or Uint8ClampedArray, Array by default
  ArrayConstructor: Array,
}

await parallel(SampleWorker, { input }, input.length, options)

// Besides worker constructor and callback to process resulting array parallel1d constructor accepts options as well
const workers = new Parallel(SampleWorker, console.log, options)

Getting Info

import Parallel from 'parallel1d'

// Get and check defaults without calling constructor with `new`
console.log('Defaults:', Parallel.DEFAULTS)

const workers = new Parallel(SampleWorker, console.log)

// Get the `numberOfWorkers` set in options or by default from a `threads` property
console.log('Threads number:', workers.threads)

// Get is it already started or no?
console.log(workers.working ? 'Work in progress' : 'We need to start it first')

Terminating

If you need to stop all workers immediately, call:

// the callback argument will be called after termination of workers
workers.terminate(callback)

Small Size

With all (0) dependencies, minified and gzipped:

  • require('parallel1d') 505 B
  • require('parallel1d/promisified') 550 B

How to Run the Demo Locally

Run in a console:

git clone [email protected]:bouvens/parallel1d.git
cd parallel1d
npm install
npm run start