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

multi-threads

v1.0.8

Published

Introducing NodeJS Multithreading Library - a high-performance, lightweight library designed to make multithreading in NodeJS simple and easy. With this library, you can take advantage of the power of multithreading to improve the performance of your Node

Downloads

14

Readme

NodeJS Multithreading Library

Introducing NodeJS Multithreading Library - a high-performance, lightweight library designed to make multithreading in NodeJS simple and easy. With this library, you can take advantage of the power of multithreading to improve the performance of your NodeJS applications. Whether you're working on a complex server-side application or just need to run multiple tasks simultaneously, this library has got you covered. The library utilizes JavaScript's built-in Worker threads to create multiple threads, each with its own event loop and memory heap. This means you can run multiple independent tasks in parallel without affecting the main thread and blocking the event loop. The library is easy to use and comes with a simple API that makes it easy to create and manage worker threads. It also includes features such as error handling and communication between threads, making it a complete solution for multithreading in NodeJS. With NodeJS Multithreading Library, you can easily speed up your applications, improve their responsiveness, and make them more scalable. So, why wait? Start taking advantage of multithreading in NodeJS today with this library!

Installation

Use the package manager npm to install multi-threads.

npm install multi-threads

Usage

This code demonstrates how to use the multi-threads module to split a task into multiple threads to speed up the execution of the task.

import thread from 'multi-threads';

//split task thread
async function threadTask(){
    let count=this.start;
    while(count<this.end){
        count++;
    }
    return count;
}


async function thread_splitter(){
    console.time('Required Time');

    const THREAD_USE=4;
    const TOTOAL_COUNT=20E+9; //20 billion

    const threads=[];
    for(let i=0;i<THREAD_USE;i++){
        //pass the split task to thread
        threads.push(thread.open(threadTask,{start:0,end:TOTOAL_COUNT/THREAD_USE}));
    }
    
    const counts=await Promise.all(threads); //await to finish the all thread process
    let total_count=counts.reduce( (accumulator, currentValue) => accumulator + currentValue,0); //summing the counts

    console.log('Counted:',total_count);
    console.timeEnd('Required Time');
}

thread_splitter();

The threadTask function is a task that will be executed in each thread. The task counts from this.start to this.end in a loop. The this keyword in the threadTask function refers to the context object that was passed when the thread was created using thread.open. The properties of the context object can be accessed using this.property_name syntax.

The thread_splitter function is responsible for creating the threads and running the task in them. It uses THREAD_USE (4) to determine the number of threads to create. The TOTOAL_COUNT is set to 20 billion and is used to determine the size of the task each thread will be responsible for by dividing TOTOAL_COUNT by THREAD_USE.

For each iteration of the loop, a new thread is created using the thread.open method, and the threadTask function is passed to it along with the start and end parameters to determine the range of numbers the thread will be responsible for counting. The created threads are stored in the threads array.

Finally, Promise.all is used to wait for all the threads to finish executing. The counts array stores the result of each thread. The total_count variable is then calculated by summing the values in the counts array using the reduce method.

The total count and the time required to complete the task are then logged to the console using console.log and console.timeEnd, respectively.

Features

  • Utilizes JavaScript's built-in Worker threads to create multiple threads
  • Each thread has its own event loop and memory heap
  • Easy to use API for creating and managing worker threads
  • Error handling and communication between threads included
  • Improves performance and responsiveness of NodeJS applications
  • Makes applications more scalable

Version History

  • v1.0.7: Initial release.

License

This package is released under the MIT License.