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

nestworker

v1.1.20

Published

A lightweight tool built on top of Node.js worker_threads, enabling multithreading.

Downloads

15

Readme

nestworker

A simple library that provides an abstraction for the Node.js worker_threads module. You can run your function in a dedicated thread by working with Promises.

Example

import { executeInThread } from 'nestworker';

async function calculate(): Promise<void> {
  const values = await Promise.all([
    executeInThread(() => 2 ** 10), // this doesn't block the main thread
    executeInThread(() => 3 ** 10),
  ]);

  console.log(values); // [1024, 59049]
}

calculate();

This example demonstrates the optimization of two resource-intensive calculations through parallel execution in distinct threads. By distributing the tasks across separate threads, significant time savings are achieved.

Nestworker's takes a task function as its parameter, orchestrates its execution in a new thread, and subsequently delivers a Promise.

Surprisingly simple, isn't it?

Installation

$ npm i nestworker

All examples:

API

executeInThread(task, { args: any[] }

Runs the specified function in a separate thread.

Parameters

  • Task (Function): The function to be executed in a thread.
    • This can also be a async function (promise).
  • ...params (Any): Additional arguments to be passed to the Task function.
    • Parameter cann't be a function.
const task = function(a: number, b: object, c: boolean) { ... };
executeInThread(task, { args: [1, {}, true] })

The executeInThread function allows you to execute a given task function in a dedicated thread, similar to the behavior of setTimeout or setInterval. You provide the main function to be executed, along with any additional arguments (...args) that should be passed to the given function.

Returns

Promise<any>: A Promise that resolves with the return value of the callback.

Inside the provided function, you have the flexibility to return any value, including a Promise. The returned value, whether it's a standard value or a Promise, will be passed back to you as the resolved result of the Promise returned by the executeInThread function.

const number = await executeInThread(() => 123); // 123
const name = await executeInThread(() => Promise.resolve('John')); // John

Important (limitation)

Access to data outside of the task function is restricted. If you require the use of a module, it should be required within the task function. The sole method for accessing data within a task function from external sources is through the utilization of the parameters. Closures do not function in this context.

In this example, we're reading a file in a separate thread and returning the data in string format. We start by defining a task function that will run within the thread, and then we prepare the necessary parameters to be passed as inputs to that function.

import { executeInThread } from 'nestworker';

async function task(filename: string) {
// Closure doesn't work here
  const { readFile } = await import('fs/promises');
  const content = await readFile(filename);
  return content.toString();
}

async function read() {
  const content = await executeInThread(task, { args: [filename] });
  console.log(content);
}

read();

There is also another option if you don't want to use import inside the function.

import { executeInThread } from 'nestworker';

// this will be executed in a dedicated thread
async function task(modules: { 'fs/promises': typeof import('fs/promises') }) {
  // Closure doesn't work here
  const { readFile } = modules['fs/promises'];

  const content = await readFile(__filename);

  return content.toString();
}

async function read() {
  const content = await executeInThread(task, {
    threadModules: ['fs/promises'],
  });

  console.log(content);
}

read();

The threadModules parameter is an array of strings that represent the modules you want to use in the thread. The modules will be imported and passed to the task function first argument as an object.

Contributing

See the contributing guide for detailed instructions on how to get started with our project.

Author

Vahe Hakobyan: Telegram

License

Licensed under MIT.