Introducing npm Teams! Private packages + team management tools.Learn more »

fast-cartesian

4.1.0 • Public • Published

Codecov Travis Node Gitter Twitter Medium

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.

Example

const fastCartesian = require('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' ] ]
// ]

Demo

You can try this library:

Install

npm install fast-cartesian

API

fastCartesian(inputs)

inputs: Array<Array>
Return value: Array<Array>

Returns a two-dimensional Array where each row is a combination of inputs.

Benchmarks

The following benchmarks compare the performance of this library against alternatives (big-cartesian, cartesian-product, fast-cartesian-product, power-cartesian-product, cartesian and lodash.product).

## fast-cartesian ######################
1 array                           0.87ms
2 arrays                          1.02ms
4 arrays                          1.22ms
8 arrays                          1.43ms
16 arrays                         3.49ms

## fast-cartesian-product ##############
1 array                           1.13ms
2 arrays                          1.48ms
4 arrays                          2.61ms
8 arrays                          4.48ms
16 arrays                        10.47ms

## big-cartesian #######################
1 array                           6.94ms
2 arrays                          7.41ms
4 arrays                          7.92ms
8 arrays                          8.71ms
16 arrays                        17.11ms

## power-cartesian-product #############
1 array                           4.81ms
2 arrays                          5.29ms
4 arrays                          7.44ms
8 arrays                         11.51ms
16 arrays                        19.60ms

## cartesian-product ###################
1 array                           2.57ms
2 arrays                          3.68ms
4 arrays                         10.79ms
8 arrays                         14.33ms
16 arrays                        20.79ms

## cartesian ###########################
1 array                           6.39ms
2 arrays                         16.54ms
4 arrays                         17.69ms
8 arrays                         22.52ms
16 arrays                        34.37ms

## lodash.product ######################
1 array                          38.81ms
2 arrays                         40.43ms
4 arrays                         43.31ms
8 arrays                         54.85ms
16 arrays                        82.06ms

Support

If you found a bug or would like a new feature, don't hesitate to submit an issue on GitHub.

For other questions, feel free to chat with us on Gitter.

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 button (pencil icon) and suggest a correction.

If you would like to help us fix a bug or add a new feature, please check our guidelines. Pull requests are welcome!

ehmicky
ehmicky

💻 🎨 🤔 📖

Install

npm i fast-cartesian

DownloadsWeekly Downloads

56

Version

4.1.0

License

Apache-2.0

Unpacked Size

30.9 kB

Total Files

11

Homepage

git.io/fjRR4

Last publish

Collaborators

  • avatar