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

@jsweb/worker

v1.1.7

Published

JavaScript module to parallel process data through dynamic multi-thread workers.

Downloads

57

Readme

@jsweb/worker

JavaScript module to parallel process data through dynamic multi-thread workers.

Installation

NPM or Yarn

The most common way used for the modern web applications development:

npm i -S @jsweb/worker

yarn add @jsweb/worker

And at your JS you can import the module that fits your environment:

// ES6+ frontend web dev to use worker at the browser
import worker from '@jsweb/worker'

// ES6+ backend dev to use worker in Node.js
import worker from '@jsweb/worker/dist/node.mjs'

// CommonJS backend dev to use worker in Node.js
const worker = require('@jsweb/worker/dist/node.js')

Yes, it works in Node.js, but you must to import/require the correct module for your dev environment.

CDN

If you prefer, you can get it directly from Unpkg CDN to use it in web frontend.

<script src="https://unpkg.com/@jsweb/worker"></script>

By including the script in your page as traditional JS, the jsWebWorker function becomes available at the global scope.

This main exported module is in UMD format and targets browser.

But if you want to use a modern ES6+ module, you can do this:

<script type="module">
  import worker from 'https://unpkg.com/@jsweb/worker/dist/browser.js'

  // Your code goes here...
</script>

Usage example

import worker from '@jsweb/worker'

let result

const data = [...aLotOfItemsHere]

worker(data)
  .env('two', 2)
  .map((item) => item.value ** self.env.two)
  .reduce((value, sum) => value + sum, 0)
  .finally((sum) => (result = sum))

Important:

Notice the self thing into the code!

Any function task running at the Worker must refer to the environment as self to access other context resources, except for globals.

The worker

Instance

The worker function returns a high level object instance that works like Promise, with async methods.

In fact, it uses a Promise internaly to guide the entire process data flow when methods are called.

You can chain or async/await them to get the results.

The worker instance receive 2 arguments:

  1. data or value to process (required)
  2. a number to setup concurrent threads to process data (optional)
worker(data, 4).exec(...)

The worker tries to detect how much "hardware concurrency threads" are available to use, or use the default: 2.

Environment (scope)

At the browser, Workers run in parallel hardware threads, detached from main JS application thread.

It means you don't have access to all props and features available at the global JS scope.

If you need to know more about this, try to start here: Web Workers API : Functions and interfaces available in workers.

At the Node.js environment, workers use child_process. To know more about this, go to Node.js API: child_process

Methods

All methods are asyncronous and return the worker instance to chain other methods, except the .finally method, that ends the flow worker and returns just a native Promise.

.env(key, value)

Sets a Worker environment constant attached to 'self.env' object.

  • key must to be a valid object key
  • value can be any data to be available at Worker environment

Inside your tasks functions, you can refer to these constants through 'self.env' object.

worker(data)
  .env('x', 5)
  .env('y', 25)
  .exec((value) => value * self.env.x * self.env.y)

.imports(...args)

Imports code to use at Worker enviroment like functions or scripts.

Accepts any number of named functions or scripts URLs.

const moment = 'https://momentjs.com/downloads/moment.min.js'

function howManyTimeAgo(dt) {
  return self.moment(dt).fromNow()
}

function ageCalc({ birthDate }) {
  return self.howManyTimeAgo(birthDate)
}

worker(data).imports(moment, howManyTimeAgo).exec(ageCalc)

It's just a "dumb" example... But it's important to remember about Worker scope!

.unpkg(...args)

Import NPM modules from UNPKG CDN to use at Worker environment.

Accepts any number of string arguments to identify modules.

function createKeysForAllItems(item) {
  item.key = self.randkey.uuid()

  return item
}

worker(data).unpkg('randkey').map(createKeysForAllItems)

.exec(task)

Executes a task function in a single parallel thread.

worker(data)
  .env('x', 5)
  .env('y', 25)
  .exec((value) => value * self.env.x * self.env.y)

.map(task)

Executes a multi-thread Array.map method.

worker(data)
  .env('two', 2)
  .map((item) => item.value ** self.env.two)

.reduce(task, arg)

Executes a multi-thread Array.reduce method.

worker(data).reduce((value, sum) => value + sum, 0)

.filter(task)

Executes a single parallel thread Array.filter method.

worker(data).filter((value) => value.includes('OK'))

.sort(task)

Executes a single parallel thread Array.sort method.

worker(data).sort((a, b) => a - b)

.flatMap(task)

Executes a multi-thread Array.flatMap method.

const data = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  // ...
]

worker(data)
  .env('two', 2)
  .faltMap((item) => item.value ** self.env.two)

.then(task)

Promise-like async step.

Unlike the native Promise.then, which returns a Promise, this method returns the worker instance to chain another methods.

worker(data)
  .filter((value) => value.includes('OK'))
  .then(doSomeThing)

.finally(task)

Promise-like async last step.

Like the native Promise.finally, this method returns a Promise and stops the worker instance flow.

You can't chain other worker methods after this.

worker(data)
  .filter((value) => value.includes('OK'))
  .then(doSomeThing)
  .finally(doLastThing)

How this module works?

Coming soon...