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

parallel-memo-dom

v1.1.0

Published

A browser library for offloading heavy computations to web workers with memoization

Downloads

124

Readme

Parallel-Memo-DOM

Parallel-Memo-DOM is a powerful browser library for offloading heavy computations to web workers, enabling parallel execution and improving application performance. It includes advanced features such as memoization, dynamic thread pool management, and improved type safety.

Features

  • Parallel execution of functions using Web Workers
  • Thread pool management for efficient resource utilization
  • LRU (Least Recently Used) caching mechanism for improved performance
  • Support for transferable objects for efficient data transfer
  • Dynamic thread pool sizing based on available hardware
  • Improved type safety with TypeScript generics
  • Error handling and recovery for worker failures

Installation

npm install parallel-memo-dom

Usage

Basic Usage with Thread

import { Thread } from 'parallel-memo-dom';

const someHeavyComputation = (a: number, b: number): number => {
    // Simulate heavy computation
    let result = 0;
    for (let i = 0; i < 1000000000; i++) {
        result += Math.sqrt(a * b);
    }
    return result;
};

(async () => {
    try {
        const result = await Thread.exec(someHeavyComputation, 10, 20);
        console.log('Computation result:', result);
    } catch (error) {
        console.error('Error in thread execution:', error);
    }
})();

Using Thread Pool

import { ThreadPool } from 'parallel-memo-dom';

const pool = new ThreadPool({ size: 4 });

const someHeavyComputation = (a: number, b: number): number => {
    // Simulate heavy computation
    let result = 0;
    for (let i = 0; i < 1000000000; i++) {
        result += Math.sqrt(a * b);
    }
    return result;
};

(async () => {
    try {
        const result = await pool.exec(someHeavyComputation, 10, 20);
        console.log('Computation result:', result);
    } catch (error) {
        console.error('Error in thread execution:', error);
    }
})();

Configuring Caching

import { Thread } from 'parallel-memo-dom';

// Disable caching if needed
Thread.configure({ enableCaching: false });

// Rest of the code remains the same

Using with Transferable Objects

import { Thread } from 'parallel-memo-dom';

const processBigData = (data: ArrayBuffer): ArrayBuffer => {
    // Process the data
    const result = new ArrayBuffer(data.byteLength);
    new Uint8Array(result).set(new Uint8Array(data));
    return result;
};

const bigData = new ArrayBuffer(1000000);

(async () => {
    try {
        const result = await Thread.exec(processBigData, bigData);
        console.log('Processed data size:', result.byteLength);
    } catch (error) {
        console.error('Error in thread execution:', error);
    }
})();

API

Thread

  • static configure(options: { enableCaching?: boolean }): void: Configures the caching behavior of the library.
  • static exec<T extends any[], R>(fn: (...args: T) => R, ...args: T): Promise<R>: Executes the provided function in a web worker with the given arguments and returns a promise that resolves with the result.

ThreadPool

  • constructor(options: { size: number, enableCaching?: boolean }): Creates a new thread pool with the specified size and optional caching.
  • exec<T extends any[], R>(fn: (...args: T) => R, ...args: T): Promise<R>: Executes the provided function in a web worker from the pool with the given arguments and returns a promise that resolves with the result.

Advanced Features

Dynamic Thread Pool Sizing

The thread pool automatically adjusts its size based on the available hardware concurrency. This ensures optimal performance across different devices.

LRU Caching

The library uses an LRU (Least Recently Used) caching mechanism to store results of previously executed functions. This can significantly improve performance for repeated calls with the same arguments.

Improved Type Safety

The library now uses TypeScript generics for better type inference and safety when working with different function signatures.

Vite Configuration

If you are using Vite and encounter issues with the parallel-memo-dom dependency, you can exclude it from the dependency optimization process by adding the following configuration to your vite.config.ts:

import { defineConfig } from 'vite';

export default defineConfig({
  optimizeDeps: {
    exclude: ['parallel-memo-dom']
  }
});

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

License

This project is licensed under the MIT License. See the LICENSE file for details.