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

@lukeschaefer/worker-bee

v1.0.5

Published

Tiny utility for pushing tasks into an async Web Worker

Downloads

7

Readme

WorkerBee

WorkerBee is a minimal TypeScript library to make using Web Workers as easy as possible. Instead of having to create a seperate file for code that is run in a different thread, workers can be made inline with their own functions and be communicated with asynchronously.

Why?

Javascript is single-threaded. If a complex operation is running for a while, nothing else will run, and your browser will slow down, and your UI will become unresponsive.

Here's a quick example of a function that can run for a long time if you're not careful:

// From https://stackoverflow.com/a/57012040:
const findNthPrime = (n : number) => {
  const primes = [];
  let i = 1;
  while (i++ && primes.length < n) {
    primes.reduce((a,c)=>(i%c)*a,2) && prime.push(i);
  }
  return primes.pop();
}

// Will likely cause your browser to become unresponsive:
const result = findNthPrime(100000);

With WorkerBee - if you have a simple, self-contained function like that, you can execute it another thread like so:

const workerFindNthPrime = miniWorker(findNthPrime);
const result = await workerFindNthPrime(100000);

And things will work nice and smoothly, no matter how long that operation takes. See below for more complex use-cases.

Installation

npm install @lukeschaefer/worker-bee

Usage:

There are two ways of creating workers in WorkerBee:

miniWorker()

Like we saw above, simply give miniWorker a self-contained function, and it can execute it in a separate thread:

import { miniWorker } from '@lukeschaefer/worker-bee';

const multiplier = miniWorker((x, y) => {
  return x * y;
});

const result = await multiplier(12,3);
console.log(result); // 36 is logged

But that's about all it can do.

createWorker

createWorker takes in an initialization object, which can consist of properties and methods to call. It will generate accessors and setters so that you can interact with your Web Worker as easily as a normal object:


const usefulWorker = createWorker({
  counter: 0,
  addToCounter: function(input: number) {
    this.counter += input;
    return this.counter;
  },
  addRandomToCounter: function() {
    return this.addToCounter(Math.random());
  }
});

usefulWorker.counter = 20;

usefulWorker.addToCounter(10); // Promise<30>
usefulWorker.addRandomToCounter(); // Promise<30.123>

createWorker workers come with a few helper functions as well:

  • destroy() - Closes the webworker.
  • importScript() - Makes the webworker call loadScript