Fast cartesian product
npm install fast-cartesian






Fast cartesian product.
Retrieves every possible combination between several arrays
(cartesian product).
Fastest available library in JavaScript.
When producing millions of combinations or combining hundreds of arrays,big-cartesian should be used
instead.
> We are now using this library for ourworldindata.org and have seen an almost
> 50-fold performance increase from the naive method we used before!
>
> @MarcelGerber
``js
import fastCartesian from 'fast-cartesian'
console.log(
fastCartesian([
['red', 'blue'],
['circle', 'square'],
]),
)
// [
// [ 'red', 'circle' ],
// [ 'red', 'square' ],
// [ 'blue', 'circle' ],
// [ 'blue', 'square' ]
// ]
// Return initial indexes
console.log(
fastCartesian(
[
['red', 'blue'],
['circle', 'square'],
].map(Object.entries),
),
)
// [
// [ [ '0', 'red' ], [ '0', 'circle' ] ],
// [ [ '0', 'red' ], [ '1', 'square' ] ],
// [ [ '1', 'blue' ], [ '0', 'circle' ] ],
// [ [ '1', 'blue' ], [ '1', 'square' ] ]
// ]
`
``
npm install fast-cartesian
This package works in both Node.js >=18.18.0 and
browsers.
This is an ES module. It must be loaded using
an import or import() statement,
not require(). If TypeScript is used, it must be configured to
output ES modules,
not CommonJS.
inputs: Array\Array
_Return value_:
Returns a two-dimensional Array where each row is a combination of inputs.
The following benchmarks compare the performance of this
library against alternatives
(big-cartesian,
cx-product,
cartesian-product,
fast-cartesian-product,
power-cartesian-product,
cartesian and
lodash.product).
`fast-cartesian ######################
1 array 1.22ms
2 arrays 1.82ms
4 arrays 3.12ms
8 arrays 1.87ms
16 arrays 4.82ms
Support
For any question, _don't hesitate_ to submit an issue on GitHub.
Everyone is welcome regardless of personal background. We enforce a
Code of conduct in order to promote a positive and
inclusive environment.
Contributing
This project was made with ❤️. The simplest way to give back is by starring and
sharing it online.
If the documentation is unclear or has a typo, please click on the page's
Edit`If you would like to help us fix a bug or add a new feature, please check our
guidelines. Pull requests are welcome!
ehmicky 💻 🎨 🤔 📖 | Marcel Gerber 💻 | Paul Heidenreich 💻 |