DEV Community

Cover image for Better Math in JavaScript?
Asadbek Karimov
Asadbek Karimov

Posted on

Better Math in JavaScript?

As a web developer working on data visualization and scientific computing projects, I've often found JavaScript's native math capabilities lacking. The hunt for robust standard library led me to stdlib-js.

The promise of stdlib

The stdlib project caught my eye with its impressive set of features:

  • Comprehensive mathematical functions
  • Focus on numerical precision
  • Cross-platform consistency
  • Extended functionality beyond native Math

Benchmarking Reality

I ran benchmarks comparing stdlib against native JavaScript Math operations across 100,000 rows of data. The results were suprising:

Performance Comparison Table

Operation Performance Difference Precision (Max Diff) Precision (Avg Diff) Notes
exp 189.44% slower 0.000e+0 0.000e+0 Largest performance gap
log10 58.94% slower 4.441e-16 1.142e-17 Significant slowdown
ln/log 33.63% slower 0.000e+0 0.000e+0 Notable performance impact
tan 31.87% slower 0.000e+0 0.000e+0 Slower trig operation
acos 29.17% slower 4.441e-16 4.569e-17 Slower inverse trig
sin 24.54% slower 1.110e-16 3.101e-18 Basic trig affected
cos 23.00% slower 1.110e-16 3.039e-18 Basic trig affected
asin 22.74% slower 2.220e-16 1.219e-17 Inverse trig impact
atan2 17.02% slower 2.220e-16 6.958e-18 Moderate slowdown
atan 13.86% slower 1.110e-16 2.675e-18 Better performing inverse trig
ceil 2.84% slower 0.000e+0 0.000e+0 Minimal impact
abs 1.99% slower 0.000e+0 0.000e+0 Negligible difference
floor 2.22% faster 0.000e+0 0.000e+0 Slight improvement
round 1.44% faster 0.000e+0 0.000e+0 Minor gain
sqrt 2.68% faster 0.000e+0 0.000e+0 Better performance
hypot 1.18% faster 0.000e+0 0.000e+0 Slight improvement

The Good

Basic operations showed modest improvements:

  • sqrt: 6.23% faster
  • round: 3.02% faster
  • floor: 2.02% faster

The Not So Good

Complex operations took a significant hit:

  • exp: 189.85% slower
  • log10: 59.37% slower
  • trig functions: 25-33% slower

Precision Trade-off

The precision differences were minimal, with a maximum difference of 4.441e-16. Most operations whoed identical results between stdlib-js and native Math.

When to Use stdlib-js

  • Basic mathematical operations
  • Projects requiring guaranteed cross-platform behavior
  • Cases where consistent results matter more than raw speed

Looking Forward

There's potential for improvement - converting stdlib-js to WebAssembly could boost performance by running close to native speed, particularly for those complex operations currently showing significant slowdown. But until that happens, the choice between stdlin-js and native Math requires careful consideration of your specific needs.

Bottom Line

stdlib-js is a solid library taht delviers on its promise of consistency and extended functionality. However, the performance trade-offs mean it's not always the best choice. For my projects, I'll be using native approach and maybe in the future will try to compile stdlib-js to WASM to test if its really faster.

Have you tried stdlib in your projects? I'd be curious to hear about your experiences with mathematical computations in JavaScript.


Resources

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (0)

This post blew up on DEV in 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay