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

thread-like

v1.2.5

Published

A tool, which allows you to make non-parallel function work like they are running in separate threads.

Downloads

18

Readme

Thread-like

A tool, which allows you to make non-parallel function work like they are running in separate threads.

npm version

Installation

npm install thread-like
<script src="https://cdn.jsdelivr.net/npm/thread-like/lib/parallelize.js"></script>

Demo

Why?

  • You need to calculate something big
  • It takes a lot of time and blocks the main thread (UI)
  • You want to make it work in parallel

For example, imagine you have to run doSmth() (executes in 1-2 ms) many times:

function longBlockingOperation() {
  let count = 0;
  while (count++ < 2500) {
    doSmth();
  }
}

If you just run it, the page will be blocked for about a second. Enough to think that something is wrong with the service.

DevTools Profiler will show something like this: the thread is blocked for 964 ms by longBlockingOperation.

Profiler with sync execution

thread-like allows you to make such function:

  • run in parallel
  • stoppable
  • non-blocking

Execution will be split into small parts:

API

parallelize(fn*, options)

import { parallelize } from "thread-like";

const config = {}; // optional

const longNonBlockingOperation = parallelize(function* longBlockingOperation() {
  let count = 0;
  while (count++ < 2500) {
    yield doSmth();
  }
  return count;
}, config);

| Config | Type | Default | Description | | ------------ | --------- | --------------------- | --------------------- | | debug | boolean | false | Show debug messages in console | | maxTime | number | null | Max time of execution in ms. If null - no limit |

  1. You should make target function a generator (function*, more)
  2. You have to mark places for splits with yield

It will be better if there will be at least 0.1 ms between two yields. Otherwise, the time for switching and controlling will be spent more resources, and the execution of the task will be slowed down many times.

Now you can run longNonBlockingOperation and it will run in pseudo-parallel mode.

await

const task1 = longNonBlockingOperation();
const task2 = await longNonBlockingOperation();

Parallelized function returns a Promise, which will be resolved when the task is completed.

abort

You can stop the task at any time:

task.abort(resolve);

It will stop the task, the promise will be resolved or rejected depending on the resolve parameter (true by default). The value in the handler will be the Aborted symbol:

import { Aborted, isAborted } from 'thread-like';
...
try {
  const task = await runTask();
} catch (e) {
  if (!isAborted(e)) { // Или e !== Aborted
    // Abort handling
  }
}

Helpers

Yield every n iterations

Allows to stop execution not every time, but every n iterations.

import { parallelize, everyNth } from 'thread-like';
...
const p = parallelize(function* () {
  let count = 0;
  while (true) {
    count++;
    yield* everyNth(count, 100);
  }
});