calmm-js / kefir.atom

Composable and decomposable reactive state with lenses and Kefir
MIT License
51 stars 6 forks source link

Bundle size #11

Closed artalar closed 6 years ago

artalar commented 6 years ago

Hi! Please pay your attention at bundle size: https://bundlephobia.com/result?p=kefir.atom@5.5.1 Bigest part of bundle just 3 methods of partial.lenses. Can u avoid it?

polytypic commented 6 years ago

Thanks for the concern!

This library is designed to work with Partial Lenses. The key feature of this library is the use of lenses to implement read-write views of state. This way state can be decomposed for UI components and UI components can be made independent of the storage of their state. So, removing the dependency would defeat the purpose of this library.

Also, the size reported by Bundle Phobia is a highly conservative estimate—it just sums the sizes of the libraries. Bundle Phobia actually says:

supports the module field. You can get smaller sizes with tree shaking.

Indeed, all of my libraries are specifically engineered to support tree-shaking / dead-code elimination. When you use a modern bundler like Webpack (with module concatenation) or Rollup and a minifier like UglifyJS, you mostly only get what you use. That is because my libraries mostly use only pure functions rather than methods. So, it is easy for a modern bundler (and UglifyJS) to discard unused functions.

Now that you mention it, when it comes to dependencies and bundle size, there are actually a couple of things that could be improved in this library. The most important of those is that the view method need not actually be a method. It would be better to turn it into a free-standing function. This would mean that unless one would then use the free-standing view function, the LensedAtom class could be dead-code eliminated. Similarly the set and remove methods could be turned into free-standing functions and would also slightly reduce bundle size in case those functions would not be explicitly used in application code.