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-burst

v1.0.0

Published

A Node.js library for multithreaded performance boosting with in-memory caching, this library aims to increase performance.

Downloads

69

Readme

ThreadBurst

This repository provides a simple and efficient implementation of a ThreadPool for handling asynchronous tasks using worker threads in Node.js. Additionally, it includes a MemoryCache to optimize task execution by caching results and preventing redundant calculations.

Features

  • ThreadPool:

    • Executes tasks in a pool of worker threads.
    • Automatically manages the number of concurrent workers based on available CPU cores or a custom maximum number of threads.
    • Tasks can be cached for faster execution if the same task and data are requested again.
  • MemoryCache:

    • Stores task results in memory with an optional time-to-live (TTL) and maximum size.
    • Automatically cleans up expired cache entries and evicts the oldest entries when the cache exceeds the maximum size.

Installation

To use the ThreadPool and MemoryCache classes in your Node.js project, follow these steps:

  1. Install the package via npm or yarn.
npm install thread-burst
yarn add thread-burst
  1. Alternatively, you can download the files and integrate them into your project.

Usage

**Example: Using the ThreadPool and MemoryCache **


import { ThreadPool, MemoryCache } from 'thread-burst';

// Create a new ThreadPool instance
const threadPool = new ThreadPool({
  maxThreads: 4, // Maximum number of threads (default is number of CPU cores)
  cache: {
    ttl: 3600000, // 1 hour TTL for cache
    maxSize: 1000, // Maximum cache size
  }
});

// Example task to run
const task = async (data: string) => {
  return new Promise<string>((resolve) => {
    setTimeout(() => {
      resolve(`Processed: ${data}`);
    }, 1000); // Simulate async work
  });
};

// Execute the task with the ThreadPool
threadPool.execute(task, 'Hello, World!')
  .then((result) => {
    console.log(result); // Output: Processed: Hello, World!
  })
  .catch((error) => {
    console.error('Error executing task:', error);
  });

Using MemoryCache to Store and Retrieve Cached Results


import { MemoryCache } from 'threadpool-memorycache';

// Create a new MemoryCache instance
const cache = new MemoryCache({
  ttl: 300000, // 5 minutes TTL
  maxSize: 500, // Maximum cache size
});

// Set an item in the cache
cache.set('key1', 'value1');

// Get an item from the cache
const value = cache.get('key1');
console.log(value); // Output: value1

// Get an item from the cache that doesn't exist
const nonExistent = cache.get('key2');
console.log(nonExistent); // Output: null

API Documentation

ThreadPool

Constructor : ThreadPool(options: { maxThreads?: number; cache?: CacheOptions })

  • maxThreads: Maximum number of worker threads in the pool. Defaults to the number of CPU cores available.
  • cache: Options for the cache (optional). Includes ttl (time-to-live in milliseconds) and maxSize (maximum number of items to store).

Method : execute<T>(task: (data: any) => Promise<T>, data: any): Promise<T>

  • task: A function that returns a Promise and takes the task data as an argument.
  • data: Data to be passed to the task function. Returns a Promise<T> that resolves with the result of the task.

Method : shutdown(): void Terminates all worker threads and clears the task queue.

MemoryCache

Constructor : MemoryCache(options: CacheOptions)

  • ttl: Time-to-live for cache items in milliseconds. Defaults to 1 hour (3600000ms).
  • maxSize: Maximum number of items the cache can store. Defaults to 1000.

Method: set(key: string, value: any, ttl?: number): void

  • key: Cache key.
  • value: Value to store.
  • ttl: Optional TTL for the cache item. Defaults to the cache's TTL.

Method: get(key: string): any | null

  • key: Cache key.
  • Returns the cached value or null if the item is expired or doesn't exist.

License

This project is licensed under the MIT License

Contributing

Feel free to fork this repository and submit pull requests for improvements or bug fixes.

  1. Fork the repository
  2. Create a new branch (git checkout -b feature/your-feature)
  3. Commit your changes (git commit -am 'Add new feature')
  4. Push to the branch (git push origin feature/your-feature)