{"id":13725529,"url":"https://github.com/lukeed/wrr","last_synced_at":"2025-10-09T18:23:41.985Z","repository":{"id":57127203,"uuid":"215663728","full_name":"lukeed/wrr","owner":"lukeed","description":"A tiny (148B) weighted round robin utility","archived":false,"fork":false,"pushed_at":"2019-11-27T00:00:17.000Z","size":11,"stargazers_count":164,"open_issues_count":0,"forks_count":2,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-10-31T10:37:42.562Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lukeed.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-16T23:40:02.000Z","updated_at":"2024-04-15T10:18:27.000Z","dependencies_parsed_at":"2022-08-31T17:20:50.395Z","dependency_job_id":null,"html_url":"https://github.com/lukeed/wrr","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fwrr","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fwrr/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fwrr/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukeed%2Fwrr/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lukeed","download_url":"https://codeload.github.com/lukeed/wrr/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224645400,"owners_count":17346146,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T01:02:26.184Z","updated_at":"2025-10-09T18:23:36.946Z","avatar_url":"https://github.com/lukeed.png","language":"JavaScript","readme":"# wrr [![build status](https://badgen.net/github/status/lukeed/wrr)](https://github.com/lukeed/wrr/actions) [![codecov](https://badgen.net/codecov/c/github/lukeed/wrr)](https://codecov.io/gh/lukeed/wrr)\n\n\u003e A tiny (148B) weighted round robin utility\n\nAt its core, a \"weighted round robin\" (wrr) will skew a list's random selection towards list items that have more _weight_ given to them.\u003cbr\u003eThis is generally seen in load-balancing contexts, but `wrr` is not at all limited to that scenario.\n\nFrom the [NGINX glossary](https://www.nginx.com/resources/glossary/round-robin-load-balancing/):\n\n\u003e **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.\n\nThis module exposes three module definitions:\n\n* **ES Module**: `dist/wrr.mjs`\n* **CommonJS**: `dist/wrr.js`\n* **UMD**: `dist/wrr.min.js`\n\n\n## Install\n\n```\n$ npm install --save wrr\n```\n\n\n## Usage\n\n\u003e Related to the NGINX example above\n\n```js\nimport wrr from 'wrr';\n\nconst servers = [\n\t// 3x capacity of B; picked 3x more often than B\n\t{ item: 'Server A', weight: 3 },\n\t// our \"base unit\" for comparison\n\t{ item: 'Server B', weight: 1 },\n\t// 2x capacity of B; picked 2x more often than B\n\t{ item: 'Server C', weight: 2 },\n];\n\n// Create reusable instance\nconst toPickServer = wrr(servers);\n\ntoPickServer(); //=\u003e 'Server A'\ntoPickServer(); //=\u003e 'Server C'\ntoPickServer(); //=\u003e 'Server A'\ntoPickServer(); //=\u003e 'Server A'\ntoPickServer(); //=\u003e 'Server A'\ntoPickServer(); //=\u003e 'Server A'\ntoPickServer(); //=\u003e 'Server C'\ntoPickServer(); //=\u003e 'Server A'\ntoPickServer(); //=\u003e 'Server A'\ntoPickServer(); //=\u003e 'Server B'\ntoPickServer(); //=\u003e 'Server C'\ntoPickServer(); //=\u003e 'Server A'\n```\n\n\n## API\n\n### wrr(items)\nReturns: `Function`\n\nReturns the function that should be used to select from your `items`.\u003cbr\u003eYou should only call `wrr` when your `items` change.\n\n#### items\nType: `Array`\n\nThe candidates for selection, each of which must be an object of `Weighted` shape:\n\n```js\ninterface Weighted\u003cT\u003e {\n\t/** The item's weight (non-decimal) */\n\tweight: number;\n\t/** The array item */\n\titem: T;\n}\n```\n\nYou can use any rubric for your `weight` value; however, only whole-number integers are allowed.\n\nThe `item` key can hold _any_ value you'd like. This is what's returned to you directly.\n\n## License\n\nMIT © [Luke Edwards](https://lukeed.com)\n","funding_links":[],"categories":["JavaScript","Packages"],"sub_categories":["Others"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukeed%2Fwrr","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flukeed%2Fwrr","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukeed%2Fwrr/lists"}