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

@grconrad/weighted-promise-pool

v1.0.4

Published

Weighted promise processing for Node.js

Downloads

5

Readme

weighted-promise-pool

A traditional promise pool manages task concurrency as the number of unresolved promises (async tasks) created by the caller. When the caller kicks off the the next async task, the concurrency count gets bumped by 1. When a promise finishes, the pool decrements the count by 1. All async tasks are treated as equals and assigned an effective weight of 1 by the pool.

weighted-promise-pool expands the notion of concurrency, definining it as a total weight of async tasks in flight. The consumer provides the weight of each task to the pool just after triggering the task. The consumer can also take into account the total weight when deciding whether to trigger more async tasks, whenever an async task completes (when the pool invokes the callback).

In practice, this notion of weight might be a proxy for resource consumption, i.e. the number of CPU cores or a heuristic that approximates computational complexity of the async task.

Example: Consumer knows the host environment can support a total weight of 30. Currently, the pool is tracking that the outstanding tasks have a total weight of 27. This leaves only an available weight of 30 - 27 = 3 (capacity). In this state, the consumer can decide not to hand over the next async task whose weight is 5 (since doing so would exceed the available capacity), or it can select a different async task with a weight <= 3 and begin that task and hand it over the pool.

Examples

import {
  WeightedPromisePool,
  WeightedTask,
  Decision,
} from '@grconrad/weighted-promise-pool';

const maxWeight = 20;

const primes = [2, 3, 5, 7, 11, 13];

let idx = 0;

const tasksReturner = (currentWeight: number): Decision<number> => {
  // Find all the primes we can process now

  if (idx >= primes.length) {
    // No more primes left to process
    return null;
  }

  let availableWeight = maxWeight - currentWeight;
  const newTasks: WeightedTask<number>[] = [];

  while (idx < primes.length) {
    // Can next prime be processed now?
    // Just for testing purposes, define the processing weight as the prime's value
    const weight = primes[idx];

    if (weight > availableWeight) {
      // Can't process the next prime yet with the available budget
      break;
    }

    // We'll take it!
    const prime = primes[idx];
    newTasks.push({
      weight,
      promise: new Promise<number>((resolve) => {
        setTimeout(() => {
          resolve(prime * prime);
        }, 100);
      }),
    });

    idx += 1;
    availableWeight -= weight;
  }

  if (newTasks.length > 0) {
    // We started new tasks, so hand them to the pool
    return newTasks;
  }

  // There are more async tasks but we can't start them yet
  return 'wait';
};

const pool = new WeightedPromisePool<number>(maxWeight, tasksReturner);

const { results: squares } = await pool.run();