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

disc-dist

v1.0.1

Published

Sample arbitrary discrete probability distributions. Extremely fast.

Downloads

9

Readme

DiscDist (Discrete Distribution)

DiskDist accepts an array of numbers, and returns a random index from that array, with a probability distribution which matches the elements in the array. DiskDist is highly performant (when used correctly).

For example:

[ 1, 2, 1 ] will have: 

  a 25% chance of returning 0, 
  a 50% chance of returning 1,
  a 25% chance of returning 2

Installation

$ npm i disc-dist

Usage

One-off usage

Only use this approach if you know you only plan to query the distribution one time.

const discDist = require("disc-dist")

let randomIndex = discDist.randomSingleUse(dist)

Repeated usage

Use this approach if it's possible that you will query the distribution more than one time.

This function has many optimizations which are only appropriate for repeated use, and which can provide substantial performance improvements.

const discDist = require("disc-dist")
let multiuse = discDist.randomMultiUse(dist)

// then, as many times as you want:
let randomIndex = multiuse.random()

Performance

DiskDist runs in O(n) in the worst case.

Because of the random nature of the algorithm, the average case can be much better depending on your distribution.

Distributions are auto-optimized to provide the fastest execution time.

The performance data below was measured on my 2018 Macbook Pro: MacBook Pro (13-inch, 2018, Four Thunderbolt 3 Ports), 2.7 GHz Intel Core i7.

You can measure your own performance numbers by running: node ./benchmarks/.

| Distribution Elements | Exection Time | Ops / Sec | | --------------------- | ------------- | ----------- | | 10 | 207ns | 4,819,678 | | 100 | 222ns | 4,502,697 | | 1,000 | 0.578us | 1,727,480 | | 10,000 | 3.895us | 256,733 | | 100,000 | 36.63us | 27,295 | | 1,000,000 | 0.412ms | 2427 | | 10,000,000 | 3.947ms | 253 |

A plot of the above data:

Execution Time Plot