ehmicky / fast-cartesian Goto Github PK
View Code? Open in Web Editor NEWFast cartesian product
License: Apache License 2.0
Fast cartesian product
License: Apache License 2.0
The title says it all. Going from a default export to a named export breaks code.
Next time please bump the major version when making breaking changes. You may wish to bump to 2.0.0 so others won't run into this issue when upgrading their dependencies.
This prevents the module to be accidentally loaded in a CommonJS context since Node will resolve the ESM files by its extension, so we can mix both world
Just rename the source files to mjs
4 algorithms I wrote to test https://github.com/fisker/fast-cartesian-product/tree/master/src/algorithms
result:
$ node -r esm benchmark/benchmarkify.js
================================
Cartesian product algorithms
================================
Platform info:
==============
Windows_NT 10.0.18362 x64
Node.JS: 10.16.2
V8: 6.8.275.32-node.54
Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz × 8
Suite: sets: 256x1
√ Calculate From Index 860,483 rps
√ Dynamic Loop Function 40,865 rps
√ Indexes Array 107,186 rps
√ Recursive 243,579 rps
√ fast-cartesian(3rd-party) 170,112 rps
Calculate From Index 0% (860,483 rps) (avg: 1μs)
Dynamic Loop Function -95.25% (40,865 rps) (avg: 24μs)
Indexes Array -87.54% (107,186 rps) (avg: 9μs)
Recursive -71.69% (243,579 rps) (avg: 4μs)
fast-cartesian(3rd-party) -80.23% (170,112 rps) (avg: 5μs)
-----------------------------------------------------------------------
Suite: sets: 1x256
√ Calculate From Index 162,394 rps
√ Dynamic Loop Function 234,190 rps
√ Indexes Array 76,094 rps
√ Recursive 145,094 rps
√ fast-cartesian(3rd-party) 69,551 rps
Calculate From Index -30.66% (162,394 rps) (avg: 6μs)
Dynamic Loop Function 0% (234,190 rps) (avg: 4μs)
Indexes Array -67.51% (76,094 rps) (avg: 13μs)
Recursive -38.04% (145,094 rps) (avg: 6μs)
fast-cartesian(3rd-party) -70.3% (69,551 rps) (avg: 14μs)
-----------------------------------------------------------------------
Suite: sets: 4x8
√ Calculate From Index 4,292 rps
√ Dynamic Loop Function 10,656 rps
√ Indexes Array 2,267 rps
√ Recursive 6,919 rps
√ fast-cartesian(3rd-party) 4,726 rps
Calculate From Index -59.73% (4,292 rps) (avg: 233μs)
Dynamic Loop Function 0% (10,656 rps) (avg: 93μs)
Indexes Array -78.73% (2,267 rps) (avg: 441μs)
Recursive -35.08% (6,919 rps) (avg: 144μs)
fast-cartesian(3rd-party) -55.65% (4,726 rps) (avg: 211μs)
-----------------------------------------------------------------------
Suite: sets: 8x4
√ Calculate From Index 49 rps
√ Dynamic Loop Function 111 rps
√ Indexes Array 35 rps
√ Recursive 54 rps
√ fast-cartesian(3rd-party) 40 rps
Calculate From Index -55.54% (49 rps) (avg: 20ms)
Dynamic Loop Function 0% (111 rps) (avg: 9ms)
Indexes Array -68.66% (35 rps) (avg: 28ms)
Recursive -51.35% (54 rps) (avg: 18ms)
fast-cartesian(3rd-party) -63.51% (40 rps) (avg: 24ms)
-----------------------------------------------------------------------
Suite: sets: 2x16
√ Calculate From Index 113,161 rps
√ Dynamic Loop Function 213,381 rps
√ Indexes Array 51,884 rps
√ Recursive 130,157 rps
√ fast-cartesian(3rd-party) 82,105 rps
Calculate From Index -46.97% (113,161 rps) (avg: 8μs)
Dynamic Loop Function 0% (213,381 rps) (avg: 4μs)
Indexes Array -75.68% (51,884 rps) (avg: 19μs)
Recursive -39% (130,157 rps) (avg: 7μs)
fast-cartesian(3rd-party) -61.52% (82,105 rps) (avg: 12μs)
-----------------------------------------------------------------------
Suite: sets: 16x2
√ Calculate From Index 33 rps
√ Dynamic Loop Function 77 rps
√ Indexes Array 24 rps
√ Recursive 40 rps
√ fast-cartesian(3rd-party) 39 rps
Calculate From Index -56.68% (33 rps) (avg: 30ms)
Dynamic Loop Function 0% (77 rps) (avg: 13ms)
Indexes Array -68.1% (24 rps) (avg: 40ms)
Recursive -47.91% (40 rps) (avg: 25ms)
fast-cartesian(3rd-party) -48.62% (39 rps) (avg: 25ms)
-----------------------------------------------------------------------
Suite: sets with random length: [3, 6, 4, 6, 4]
√ Calculate From Index 7,019 rps
√ Dynamic Loop Function 23,435 rps
√ Indexes Array 4,308 rps
√ Recursive 15,024 rps
√ fast-cartesian(3rd-party) 10,586 rps
Calculate From Index -70.05% (7,019 rps) (avg: 142μs)
Dynamic Loop Function 0% (23,435 rps) (avg: 42μs)
Indexes Array -81.62% (4,308 rps) (avg: 232μs)
Recursive -35.89% (15,024 rps) (avg: 66μs)
fast-cartesian(3rd-party) -54.83% (10,586 rps) (avg: 94μs)
-----------------------------------------------------------------------
Done in 372.21s.
test against calculate-from-index.js
(default export of fast-cartesian-product
), this module is still not the fastest in some case
result:
$ node -r esm benchmark/benchmarkify.js
================================
Cartesian product algorithms
================================
Platform info:
==============
Windows_NT 10.0.18362 x64
Node.JS: 10.16.2
V8: 6.8.275.32-node.54
Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz × 8
Suite: sets: 256x1
√ Calculate From Index 771,100 rps
√ fast-cartesian(3rd-party) 163,695 rps
Calculate From Index 0% (771,100 rps) (avg: 1μs)
fast-cartesian(3rd-party) -78.77% (163,695 rps) (avg: 6μs)
-----------------------------------------------------------------------
Suite: sets: 1x256
√ Calculate From Index 156,300 rps
√ fast-cartesian(3rd-party) 77,008 rps
Calculate From Index 0% (156,300 rps) (avg: 6μs)
fast-cartesian(3rd-party) -50.73% (77,008 rps) (avg: 12μs)
-----------------------------------------------------------------------
Suite: sets: 4x8
√ Calculate From Index 4,127 rps
√ fast-cartesian(3rd-party) 4,315 rps
Calculate From Index -4.36% (4,127 rps) (avg: 242μs)
fast-cartesian(3rd-party) 0% (4,315 rps) (avg: 231μs)
-----------------------------------------------------------------------
Suite: sets: 8x4
√ Calculate From Index 48 rps
√ fast-cartesian(3rd-party) 38 rps
Calculate From Index 0% (48 rps) (avg: 20ms)
fast-cartesian(3rd-party) -19.64% (38 rps) (avg: 26ms)
-----------------------------------------------------------------------
Suite: sets: 2x16
√ Calculate From Index 113,724 rps
√ fast-cartesian(3rd-party) 76,654 rps
Calculate From Index 0% (113,724 rps) (avg: 8μs)
fast-cartesian(3rd-party) -32.6% (76,654 rps) (avg: 13μs)
-----------------------------------------------------------------------
Suite: sets: 16x2
√ Calculate From Index 32 rps
√ fast-cartesian(3rd-party) 40 rps
Calculate From Index -19.19% (32 rps) (avg: 30ms)
fast-cartesian(3rd-party) 0% (40 rps) (avg: 24ms)
-----------------------------------------------------------------------
Suite: sets with random length: [4, 4, 2, 5, 6]
√ Calculate From Index 12,299 rps
√ fast-cartesian(3rd-party) 18,014 rps
Calculate From Index -31.73% (12,299 rps) (avg: 81μs)
fast-cartesian(3rd-party) 0% (18,014 rps) (avg: 55μs)
-----------------------------------------------------------------------
Done in 156.88s.
Edit
button (pencil icon) and suggest a correction instead.shouldn't product
be ['e']
I get []
instead
const product = fastCartesian([[],['e']) // ['e']
import { expect } from 'chai'
import fastCartesian from 'fast-cartesian'
import { describe, it } from 'mocha'
describe('fastCartesian', () => {
it('basic', () => {
const product = fastCartesian([[], ['e']]) // ['e']
expect(product).to.be.deep.eq(['e'])
})
})
n/a, if test modules don't work you should be able to swap them for anything else
❯ npx envinfo --system --binaries --browsers --npmPackages fast-cartesian && npm explain fast-cartesian
System:
OS: macOS 11.6.5
CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Memory: 623.13 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.14.2 - ~/.asdf/installs/nodejs/16.14.2/bin/node
npm: 8.6.0 - ~/.asdf/plugins/nodejs/shims/npm
Browsers:
Chrome: 100.0.4896.127
Safari: 15.4
[email protected]
I'm using 5.1.0 because we have a problem with esm modules that I haven't had time to fix
This package uses ES modules. If you're seeing the following error message:
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module
This means you're using require()
to load this package. An import
or import()
statement should be used instead. For more information, see this blog post.
Which problem is this feature request solving?
Hi @ehmicky! We would love to use your great little tool in https://github.com/owid/owid-grapher so it can power https://ourworldindata.org. However, since our code base is entirely written in TypeScript, it would make our lives easier if you could add TS type definitions (.d.ts
) to this repo and to the published package on npm.
Describe alternatives you've considered
Of course, type definitions could also be added to https://github.com/DefinitelyTyped/DefinitelyTyped/, but I find it easier to work with types that are provided with the library itself.
Can you submit a pull request?
Yes, could do that if you're interested.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.