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

@a4turp/threads.js

v1.4.4

Published

Light and easy web-tool for accessing multiple threads via web workers.

Downloads

26

Readme

Lightweight JS tool for managing threads and concurrent task execution

Migrated from @a4turp/multithreading

Table of Contents

Important ⚠️

  • This package is getting updated and restructured frequently. Any major changes will bump the minor version.
  • If you have troubles after an update, please check the documentation of the new version or install the previous one.

Installation

    npm install --save @a4turp/threads.js

or

    pnpm install --save @a4turp/threads.js

or

    yarn add @a4turp/threads.js

Initialization

import Threads from '@a4turp/threads.js'

// Maximum number of threads is calculated as navigator.hardwareConcurrency - 1
// Set the Maximum number of threads
const threads = new Threads(12)

// Maximum number of threads can also be set after initialization
threads.maxThreadCount = 4

Running

The sequence of running tasks on different threads is straightforward.

  • Firstly, prepare the data by pushing tasks to the pool.
  • Secondly, execute tasks either sequentially or concurrently and wait for the result.

Preparation

Here is the showcase of data preparation:

import {TaskPool} from '@a4turp/threads.js'

function square(message) {
    // If no message is passed, it will be set to 2 by default
    message = message || 2
    return message * message
}

const tasks = new TaskPool()
tasks.push({method: square, message: 20}, square, {method: square, message: 0}).push({method: square, message: 10})

tasks.insert(2, {method: square, message: 30}, square).insert(0, {method: square, message: 40})

Execution

Here is the showcase of execution:

/**
 * @param                            response: any         Executed task response
 * @param                            progress: number      Progress of execution (0-100) // Helps to track the progress of execution
 */
type Callback = (message: any, progress: number) => void

enum ResponseType {
  ALL = 'ALL',                       // Returns all responses
  LAST = 'LAST'                      // Returns only the last response
}

interface Options {
    step?: Callback                  // Callback function called after each task is executed
    threads?: number                 // If in range of 1 and maximum number of threads, tasks will be tried to execute on the specified number of threads
    response?: ResponseType          // Response type
}

await threads.executeParallel(tasks, {
    //Options
    threads: 4,
    response: ResponseType.ALL,
    step: (response, progress) => console.log(progress)
})

await threads.executeSequential(tasks)

Note

  • Task are always executed in the order they are pushed or inserted to the pool. (Allows you to more control over the execution and if needed collect the results in an expected order)
  • Sequential execution runs on 1 thread and is slower than parallel execution.

API

Methods

Here is the list of available methods with their types and descriptions:

/**
 * @param                   index: number, ...task (Task|Function)[]
 * @return                  this
 * @description             Insert tasks at a specific index
 * @note                    If a task is a function, it will be converted to {method: Function, message: undefined}
 *                          Length of replaced tasks is determined by the number of passed tasks
 */
tasks.insert(2, <Task>{method: square, message: 30}, square).insert(0, {method: square, message: 40})
/**
 * @param                    ...task (Task|Function)[]
 * @return                   this
 * @description              Pushes tasks to the pool
 * @note                     If a task is a function, it will be converted to {method: Function, message: undefined}
 *                           You can push all tasks at once or one by one
 */
tasks.push({method: square, message: 20}, square, {method: square, message: 0}).push({method: square, message: 10})
/**
 *  @param                   index: number, ...task (Task|Function)[]
 *  @return                  this
 *  @description             Replace tasks from a specific index
 *  @note                    Length is determined by the number of passed tasks
 */

tasks.replace(2, {method: square, message: 30}, square)
/**
 *  @param                   index: number, length?: number
 *  @return                  this
 *  @description             Grab tasks from a specific index out of the pool
 */
tasks.grab(1, 3)
/**
 *  @return                  this
 *  @description             Remove the last task
 */
tasks.pop()
/**
 *  @return                  this
 *  @description             Remove the first task
 */
tasks.shift()
/**
 *  @param                   index: number, length?: number
 *  @return                  this
 *  @description             Remove tasks from a specific index. Length default is 1
 */
tasks.remove(2, 2)
/**
 *  @return                  this
 *  @description             Clear the pool
 */
tasks.clear()

/**
 *  @param                   tasks: TaskPool, options?: Options
 *  @return                  Promise<any[]|void>
 *  @description             Executes passed tasks on multiple threads concurrently
 */
await threads.executeParallel(tasks, <Options>{
    threads: 4,
    response: ResponseType.ALL,
    step: (response, progress) => console.log(progress)
})
/**
 *  @param                   tasks: TaskPool, options?: Options
 *  @return                  Promise<any[]|void>
 *  @description             Executes passed tasks on 1 thread sequentially
 *  @note                    As you saw earlier some tasks may have not any message. In sequential execution,
 *                           the message is passed from the previous task if it is not defined
 */
await threads.executeSequential(tasks)
/**
 *  @return                  this
 *  @description             Terminates all idle threads and clears the pool
 *  @note                    Awaits for all threads to finish their tasks
 *                           Doesn't provide any use yet because threads are terminated automatically after execution and    
 */
threads.dispose()

Properties

Additionally, you can access&modify some properties:

/**
 * @return                  Array<Task>
 * @description             Returns the pool array
 * @note                    Readonly
 */

tasks.pool
/**
 * @return                  number
 * @description             Returns the number of tasks in the pool
 * @note                    Readonly
 */

tasks.length

/**
 * @return                  number
 * @description             Returns the maximum number of threads
 */

threads.maxThreadCount = 10