
Security News
npm Introduces minimumReleaseAge and Bulk OIDC Configuration
npm rolls out a package release cooldown and scalable trusted publishing updates as ecosystem adoption of install safeguards grows.
A tiny (148B) weighted round robin utility
At its core, a "weighted round robin" (wrr) will skew a list's random selection towards list items that have more weight given to them.
This is generally seen in load-balancing contexts, but wrr is not at all limited to that scenario.
From the NGINX glossary:
Weighted round robin – A weight is assigned to each server based on criteria chosen by the site administrator; the most commonly used criterion is the server’s traffic‑handling capacity. The higher the weight, the larger the proportion of client requests the server receives. If, for example, server A is assigned a weight of 3 and server B a weight of 1, the load balancer forwards 3 requests to server A for each 1 it sends to server B.
This module exposes three module definitions:
dist/wrr.mjsdist/wrr.jsdist/wrr.min.js$ npm install --save wrr
Related to the NGINX example above
import wrr from 'wrr';
const servers = [
// 3x capacity of B; picked 3x more often than B
{ item: 'Server A', weight: 3 },
// our "base unit" for comparison
{ item: 'Server B', weight: 1 },
// 2x capacity of B; picked 2x more often than B
{ item: 'Server C', weight: 2 },
];
// Create reusable instance
const toPickServer = wrr(servers);
toPickServer(); //=> 'Server A'
toPickServer(); //=> 'Server C'
toPickServer(); //=> 'Server A'
toPickServer(); //=> 'Server A'
toPickServer(); //=> 'Server A'
toPickServer(); //=> 'Server A'
toPickServer(); //=> 'Server C'
toPickServer(); //=> 'Server A'
toPickServer(); //=> 'Server A'
toPickServer(); //=> 'Server B'
toPickServer(); //=> 'Server C'
toPickServer(); //=> 'Server A'
Returns: Function
Returns the function that should be used to select from your items.
You should only call wrr when your items change.
Type: Array
The candidates for selection, each of which must be an object of Weighted shape:
interface Weighted<T> {
/** The item's weight (non-decimal) */
weight: number;
/** The array item */
item: T;
}
You can use any rubric for your weight value; however, only whole-number integers are allowed.
The item key can hold any value you'd like. This is what's returned to you directly.
MIT © Luke Edwards
FAQs
A tiny (148B) weighted round robin utility
We found that wrr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
npm rolls out a package release cooldown and scalable trusted publishing updates as ecosystem adoption of install safeguards grows.

Security News
AI agents are writing more code than ever, and that's creating new supply chain risks. Feross joins the Risky Business Podcast to break down what that means for open source security.

Research
/Security News
Socket uncovered four malicious NuGet packages targeting ASP.NET apps, using a typosquatted dropper and localhost proxy to steal Identity data and backdoor apps.