Friday, November 10, 2023

JavaScript performance: "negative zero"

A clever JS performance exploration by creator of Angular(.js)

 Learn how JavaScript VMs like Google’s V8 engine work | Advanced JavaScript Performance Techniques | Frontend Masters

JavaScript "number" (based on IEEE 754 floating point numbers standard format)
has a separate representation for "negative 0" and "normal zero".
A bit strange.


The consequence is that a trivial expression "a = -x" is much slower than "a = 0 - x"
How much: 3x in Chrome, 5x is Safari, 10x in Firefox!
But why?
Because "negative zero" can not be encoded as an integer, and instead needs to be a floating point.
The result is that all operations that are very fast with integers now need to check data type and convert from floating point and back, and that is slow!


Learn how JavaScript VMs like Google’s V8 engine work | Advanced JavaScript Performance Techniques | Frontend Masters

visual: API architectural styles

cute visuals

 Post | Feed | LinkedIn