MikeMcl / decimal.js

An arbitrary-precision Decimal type for JavaScript
http://mikemcl.github.io/decimal.js
MIT License
6.35k stars 480 forks source link

Tree Shaking #162

Open jacobp100 opened 3 years ago

jacobp100 commented 3 years ago

What are your thoughts on making all functions ES6 exports (like add etc), then providing a 'lite' constructor with no functions defined on the prototype or as static functions constructor? This would mean people could use the exported functions directly and can tree shake.

My project uses ReScript (/ReasonML), so I just write bindings to whatever the exports are.

In the app, the calculations are done in a web worker, so pretty much requires the whole of this library. However, once calculated, the results are sent back to the client, but the only functionality I need from this library is toString.

Having this library be tree-shakable would significantly reduce the size of my client file.

jacobp100 commented 3 years ago

Thanks for the offer! I can handle doing this myself - I just wanted to know if you were interested in having it in the library. I'll aim make a copy of this library with the fix in the project I mentioned. You're welcome to copy it back if you want.

jacobp100 commented 3 years ago

My work is over at https://github.com/jacobp100/technicalc-calculator/blob/master/decimal

Still in the process of getting all the tests to pass - but a few of the suites are passing already

I get a 15kb decrease minified. I noticed function names get minified now too. But also some of that reduction comes from reducing the precision of pi and ln10

MikeMcl commented 3 years ago

I'm committed to keeping this library ES3 compatible, but I'll take a look. It's a good idea.

jacobp100 commented 3 years ago

Have you looked into adding build tools to this? It should be possible to write just one version (ES3 but with ES6 imports/exports), then produce an ES3-only bundle. That's something I could help with if you need!

MikeMcl commented 3 years ago

Yes, that has been considered, but at the moment I prefer to just include a separate ES module file, decimal.mjs.

jakub791 commented 2 years ago

I'm committed to keeping this library ES3 compatible, but I'll take a look. It's a good idea.

ES5 is supported everywhere now, including Internet Explorer which is like 10 years old. You do not need to support anything below that. If someone's browser does not support ES5 (or even ES6 now) then that isn't our issuse.

jakub791 commented 2 years ago

Yes, that has been considered, but at the moment I prefer to just include a separate ES module file, decimal.mjs.

Why does it use ES3 compatible syntax? Modules were added in ES6.

WuglyakBolgoink commented 1 month ago

Any updates?

MikeMcl commented 1 month ago

@WuglyakBolgoink

Yes, I have a bad cold and can't concentrate on anything.

WuglyakBolgoink commented 1 month ago

@WuglyakBolgoink

Yes, I have a bad cold and can't concentrate on anything.

get well soon!!!