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

collapsio

v1.0.1

Published

Collapse/Coalese multiple async calls into one to reduce load and bottle necks in an application

Downloads

58

Readme

collapsio.js

provides a helper function that allows the grouping\coalesing of async function calls that have cachable output. This provides more resiliance around cachable code paths that have not been cached yet.

why?

if you had a route that took 20 seconds to resolve the first time, but was cachable if 1000 users hit that route before it had a chance to resolve it would result in an attempt to resolve that route 1000 times.

collapsio will limit the number of times that route can be executed concurrently and hold the other request until it has been resolved and then passes those results onto the 1000 requests.

usage

var collapse = require('./collapsio');

collapse({}, 'key-http://google.com', request.get.bind(null, 'http://google.com'), function callback(req, res) {
});

collapse(

  • options - object
    • retryTimeout
      • default: 250
      • how long before the action is retried if it has not returned
    • maxTries
      • default: infinity
      • how many times an action is attempted
    • gracePeriod
      • default: 1000
      • how many ms the result of an action will be cached
  • key - string
    • used to identify which calls are collapsed\grouped\coallesed together
  • action - function
    • a function must be provided that takes a callback as its arguments, functions with more complex interfaces can be curried with fn.bind
  • callback - function
    • the function that is executed once the action has been completed or its results have been cached

)

example

This example will simulate 100000 requests to google.com/ over 10 seconds
  • Without a cache this would never complete.

      var collapse = require('./collapsio'),
          request = require('request'),
          url = 'http://google.com',
          start = new Date(),
          requestCount = 100000,
          requestCompleted = 0;
    
      for (var i = 0; i < requestCount; i++) {
          request.get(url, function(req, res) {
              requestCompleted++;
              if (requestCompleted === requestCount) console.log("Completed in:", new Date() - start + "ms");
          });
      }
  • With a cache a large number of requests will be created in the time it takes for the first one to return and populate the cache, and in many cases prevent the first request from actually returning.

      var collapse = require('./collapsio'),
          request = require('request'),
          url = 'http://google.com',
          start = new Date(),
          requestCount = 100000,
          requestCompleted = 0,
          cachedResponse = null;
    
      for (var i = 0; i < requestCount; i++) {
          if(!cachedResponse){
            request.get(url, function(req, res) {
                  cachedResponse = arguments;
                  requestCompleted++;
                  if (requestCompleted === requestCount) console.log("Completed in:", new Date() - start + "ms");
              });
          } else {
              requestCompleted++;
                  if (requestCompleted === requestCount) console.log("Completed in:", new Date() - start + "ms");
          }
    
      }
  • With collapsio a significantly lower number of requests are made and the result of the first successful request are passed to all of the attempts.

      var collapse = require('./collapsio'),
          request = require('request'),
          url = 'http://google.com',
          start = new Date(),
          requestCount = 100000,
          requestCompleted = 0;
    
      for (var i = 0; i < requestCount; i++) {
          collapse({}, url, request.get.bind(null, url), function(req, res) {
              requestCompleted++;
              if (requestCompleted === requestCount) console.log("Completed in:", new Date() - start + "ms");
          });
      }