js-combinatorics generates permutations and combinations as iterables
power set, combination, permutation and more in JavaScript
At a glance
- What is it?
- js-combinatorics is a small TypeScript library that turns a seed iterable into combinations, permutations, powersets, base-N and Cartesian products, all as lazy iterable objects, with BigInt-returning arithmetic helpers. The trade is that the classes materialise everything when you spread them into an array, so the size of the result is your problem, not the library's.
- Who is it for?
- js-combinatorics suits a JavaScript or TypeScript project that needs to enumerate combinations and permutations of a small set, that wants the result as an iterable rather than an eagerly built array, and that values a dependency with no runtime requirements. It does not suit a case where the result set is large, since spreading an iterator into an array builds the whole thing in memory, and it does not replace a specialised solver for constrained assignment problems.
- Can I use it commercially?
- Yes. MIT is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
- Is it still maintained?
- Yes. The repository last received commits 82 days ago.
- What is it written in?
- Mainly JavaScript, according to GitHub's language statistics.
Answers come from the project's GitHub data, last synced on October 23, 2026, and from our analysis. They are not legal advice.
Editorial analysis
Enumerating subsets is a solved problem that libraries still rewrite
The problem is one everybody hits and everybody solves again. You have a set of items and you need every subset of a given size, every ordering, or the full power set, and writing that loop correctly takes longer than reading someone else's. js-combinatorics is a JavaScript library for exactly that, described as simple combinatorics with the repository description naming power set, combination, permutation and more. The intended user is a developer who needs the enumeration rather than the theory, and the API design reflects that, with everything exposed as iterable objects you can loop over. The project also points Swift programmers at a sibling library, swift-combinatorics, with the comment that it is more naturally implemented with generics and protocol, which tells you the author treats this as a multi-language effort rather than a JavaScript-only library.
The synopsis is a class, a seed and a size
The whole API is visible in the opening example. You import the module, construct a Combination from a seed string and a size, and iterate the result with a for of loop:
import * as $C from './combinatorics.js';
let it = new $C.Combination('abcdefgh', 4);
for (const elem of it) {
console.log(elem) // ['a', 'b', 'c', 'd'] ... ['e', 'f', 'g', 'h']The seed is any iterable, so a string works as readily as an array, and the second argument is the size of each produced element. The objects are iterable, which means the enumeration is generated as you consume it rather than assembled up front, and the README is explicit that you can either iterate with for of or turn the whole thing into an array with the spread construct. The classes on offer are Permutation, Combination, PowerSet, BaseN and CartesianProduct, and you can import them individually or take the whole module with import *. There is also a static of method on each class, so if you dislike the new keyword you can write Combination.of instead.
The arithmetic helpers always answer in BigInt
This is the detail most likely to surprise you. The arithmetic functions accept both Number and BigInt inputs, where BigInt is supported, and the README says the answers always come back as BigInt. The examples in the README show the shape of it, with permutation(24, 12) returning 1295295050649600n and combination(56, 28) returning 7648690600760440n, the trailing n marking a BigInt. That is the right call for combinatorics, since the counts overflow a double quickly, but it means any downstream arithmetic, comparison or formatting has to deal with BigInt semantics, including that you cannot mix a BigInt with a Number in an arithmetic expression. factorial(18) is 6402373705728000n, and factorial(19) is 121645100408832000n, and by then the crossover has long passed: factorial(18) is already too large for a Number to represent exactly.
factoradic and combinadic are the unusual pair
Most of the module is self-explanatory, as the README says, but the README itself flags two functions as needing more explanation. factoradic(n) returns the factorial number system representation of n, and for an array with n elements you can get its nth permutation by picking ary[i] for each i in the factoradic. combinadic() is the odd one out, because unlike the other arithmetic functions it returns a function rather than a value, and that function gives you the mth combinadic digit of n choose k. The README shows the usage pattern, with combinadic(16, 8) producing a function you then call with an index to get a combination, and combinadic(58, 29) doing the same for a much larger case. The practical value is that both representations let you address the mth result directly, without generating the ones before it.
The build ships four module formats from one TypeScript source
The packaging is the part that shows how carefully this is maintained. The package is js-combinatorics at version 2.1.4, and the package.json declares the type as module, with the main and module fields both pointing at combinatorics.js, the types at combinatorics.d.ts and sideEffects set to false so a bundler can drop it when unused. The exports map routes import to the ES module and require to the CommonJS build. The build script runs rollup and then tsc to emit declarations, and the published files are the ES module, the type definitions, and the umd and commonjs directories. The repository holds the source as combinatorics.ts with a rollup.config.mjs, and the top level also contains combinatorics.js and combinatorics.d.ts as the committed build output, which is why the version script re-runs the build and stages those four files along with the README.
You can load it from a CDN or a script tag instead
The README is explicit that you do not even have to install the package. You can import it from a CDN URL, and the versioned example points at the minified build on jsDelivr for a specific release. For a plain script tag page there is a UMD build whose global is Combinatorics:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/umd/combinatorics.min.js"></script>
<script>
// now you can access Combinatorics
let c = new Combinatorics.Combination('abcdefgh', 4);The CommonJS build exists for the same reason, since the README notes that the main combinatorics.js file is an ECMAScript module and that a UMD or CommonJS version is available if you still need one. That three-format arrangement means the library drops into a bundler project, a Node CommonJS project and a script-tag page without a bundler, which is unusually accommodating for a library this small.
Enumerating the power set is exponential, and the library does not care
The limitation to plan around is inherent to the problem rather than a bug in the implementation, and it is worth stating plainly. A power set of n items has 2 to the n members, permutations of n items have n factorial, and the library will happily try to produce either one. The objects are lazy in the sense that you can consume them one at a time, but as soon as you write the spread construct that the README recommends for turning an iterator into an array, the entire result exists in memory at once. The README's own examples are all small, a Combination of eight characters taken four at a time, and a Permutation of four characters, and the node REPL session it shows is a good illustration of the output size. If your seed set is large, the fix is to consume the iterator lazily and stop early, and to be careful about which of the two forms you use.
MIT licensed, tested on Node and Bun, with a plain Makefile
The project is MIT licensed with the LICENSE file at the root, authored by Dan Kogai, and it is marked as requiring ES2020. The CI setup is worth noting because it tells you what the author actually tests against: there is a workflow for Node.js and a separate one for Bun, both with badges in the README, and the package has no runtime dependencies at all, with the devDependencies limited to the build and test toolchain, rollup with its TypeScript and replace plugins, mocha, chai, typescript and the type packages. The Makefile is three targets, all, test and clean, each delegating to the npm script of the same name. The test script runs the build first and then mocha, and the clean script removes the four generated files with a small node one-liner. The last push to the repository was on 2026-08-02, and there are no GitHub releases, so the npm version is the only version marker to track.
Editorial conclusion
js-combinatorics suits a JavaScript or TypeScript project that needs to enumerate combinations and permutations of a small set, that wants the result as an iterable rather than an eagerly built array, and that values a dependency with no runtime requirements. It does not suit a case where the result set is large, since spreading an iterator into an array builds the whole thing in memory, and it does not replace a specialised solver for constrained assignment problems. Before adopting, check the arithmetic helpers return BigInt rather than Number, so downstream comparisons and formatting need to account for that, and note that the last push to the repository was on 2026-08-02 with no GitHub releases published.
Frequently asked questions
How do I use js-combinatorics to generate combinations?
Construct a Combination with a seed iterable and a size, then iterate it. The README's example is `new $C.Combination('abcdefgh', 4)`, and the result is iterable with for of, or convertible to an array with the spread construct. There is also a static `Combination.of` form if you prefer to avoid new.
What types do the js-combinatorics arithmetic functions return?
They always return BigInt. The README says the arithmetic functions accept both Number and BigInt inputs where BigInt is supported, and the answers always come back as BigInt, with values in the examples shown with a trailing n.
Can I use js-combinatorics without installing it?
Yes. The README shows an import from a CDN URL for a specific version, and a UMD script tag whose global is Combinatorics. The package also ships a CommonJS build, with the exports map routing require to commonjs/combinatorics.js.
What is the difference between factoradic and combinadic in js-combinatorics?
factoradic(n) returns the factorial number system representation of n, and picking ary[i] for each i in it gives the nth permutation. combinadic() instead returns a function, which when called with an index gives the mth combinadic digit of n choose k, so it addresses a combination directly rather than a permutation.
What licence is js-combinatorics under and how do I run its tests?
The project is licensed under the MIT Licence, with the licence file in the repository root. The npm test script runs the build and then mocha, and the Makefile exposes the same through a test target, with all and clean targets alongside.
Official sources
Add this badge to your README
If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.
[](https://hysenlabs.com/projects/dankogai-js-combinatorics)Was this useful?