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

poisson-process

v1.0.1

Published

Generate naturally varying time intervals to improve realism in games and to prevent thundering herds in distributed systems

Downloads

2,919

Readme

poisson-process.js

npm dependencies licence

A JavaScript library to generate naturally varying time intervals. It improves realism and natural unpredictability in your games or animations like aliens walking by a window, or cars trying to drive over your character on a busy road. It removes bottlenecks in distributed systems by adding jitter that prevents thundering herd problem. It can also simulate the frequency of chat messages, page loads or arriving emails as well as queues, traffic and earthquakes. The underlying mathematical concept is called the Poisson process.

Constant vs Poisson process

In the animation above, the blue cars drive by in constant time intervals and the red ones in more natural, randomized intervals typical for the Poisson process.

Examples – InstallationUsageAPITheoryContribute

Examples

Installation

Node.js & CommonJS

First $ npm install poisson-process and then:

var poissonProcess = require('poisson-process');

Browsers

First download poisson-process.min.js and then:

<script src="poisson-process.min.js"></script>

Require.js & Async Module Definition AMD

First download poisson-process.min.js and then:

define(['scripts/poisson-process'], function (poissonProcess) { ... });

Usage

It is simple; you specify an average call interval in milliseconds, a function to be called and then start the process.

> var p = poissonProcess.create(500, function message() {
  console.log('A message arrived.')
})
> p.start()

Now the message function will be called each 500 milliseconds in average. The delay from a previous call can vary from near 0 milliseconds to a time that is significantly longer than the given average, even though the extremes are increasingly unlikely.

The process is paused by:

> p.stop()

If you desire just numbers, generate intervals by:

> poissonProcess.sample(500)
389.33242512
> poissonProcess.sample(500)
506.58621391

The chart below displays distribution of 10000 samples taken with the average interval of 500 milliseconds. The samples are grouped in 100 ms bins and the samples above 1500 ms are not charted. While 18 % of the samples are below 100 ms, the long tail of the large intervals balances the distribution at the average of 500 ms.

10k samples at average of 500 ms

Chart: Shape of the distribution. 10000 samples grouped in 100 ms wide bins.

API

poissonProcess.create(averageIntervalMs, triggerFunction)

The create constructor takes in two parameters. The averageIntervalMs is an integer and the average interval in milliseconds to call the triggerFunction. The triggerFunction takes no parameters and does not have to return anything.

var p = poissonProcess.create(500, function message() {
  console.log('A message arrived.')
})

p.start()

Start the process; begin to call the triggerFunction.

p.start()

p.stop()

Stop the process; do not anymore call the triggerFunction.

p.stop()

poissonProcess.sample(average)

The sample generates time intervals of Poisson distributed events. It returns a number; a sample from the exponential distribution with the rate 1 / average.

Note the difference between Poisson distribution and its interval distribution. Where a sample of Poisson distribution would represent a number of events happening within a fixed time window, its interval distribution represents the time between the events. The sample method implements the latter.

> poissonProcess.sample(500)
323.02...
> poissonProcess.sample(500)
returns 941.33...
> poissonProcess.sample(500)
returns 609.86...

Theory

The poisson-process.js is based on the mathematical concept of the Poisson process. It is a stochastic process that is usually perceived in the frequency of earthquakes, arriving mail and, in general, the other series of events where a single event, like an arriving letter, does not much depend on the other events, like the preceding or following letters.

It is known that inter-arrival times of the events in a Poisson process follow an exponential probability distribution with a rate parameter r. It is also known that the multiplicative inverse of r, 1/r is the mean of the inter-arrival times. Therefore to generate an event each m milliseconds in average, we sample the exponential distribution of the rate 1/m. Sampling the exponential distribution is rather simple as it follows the rule:

Sampling from an exponential distribution

In our test suite, we proof by simulation that our sampling method forms an exponential distribution with correct mean and variance. The variance of an exponential distribution is known to be 1/(r*r). We also proof that this leads to a Poisson distributed behavior. Run the test suite by first $ npm install and then $ npm test.

A detailed and enjoyable introduction to the theory is given by Jeff Preshing at How to Generate Random Timings for a Poisson Process. Wikipedia's article Poisson point process also provides a comprehensive introduction and a set of references.

Contribute

Issues and pull request are warmly welcome. Run tests with $ npm test before submitting.

Build bundle and source maps with $ npm run build.

Versioning

Semantic Versioning 2.0.0

License

MIT License