JavaScript’s been aggressively optimized by talented engineers over a long span. Unfortunately, even for seasoned JS devs, it can be hard to know how to properly leverage JS’s performance. ReScript’s type system and compiler naturally guides you toward writing code that’s very often performant by default, with good leverage of various Just-In-Time optimizations (hidden classes, inline caching, avoiding deopts, etc).
A widespread adage to write fast JavaScript code is to write as if there’s a type system (in order to trigger JS engines’ good optimization heuristics); ReScript gives you a real one and generates code that’s friendly to optimizations by default.
Is there any detailed information about this? i.e, the Just-In-Time optimizations (hidden classes, inline caching, avoiding deopts, etc).
When you create {} objects in JS, the V8 engine creates “hidden classes” for them behind the scenes. If you follow some rules and don’t change the object shape or order of properties, the engine will reuse the same class for all your objects leading to better performance. Rescript’s type system kinda enforces this by default just due to the strictness of it
Generally, when you write in ReScript, you don’t need to think of performance. I’ve never seen it generate slow code. It’s actually usually faster than handwritten js code:
It’s idiomatic to use labeled args, while in js you usually pass an object with options (an object allocation is quite costly)
There are blocks as expressions, which safes a function call you’d usually have in Js
In idiomatic ReScript, you create data objects and functions to do operations on them. Compared to classes people usually use in Js, ReScript’s approach is faster and tree-shakeable.
Besides hidden classes mentioned by @hellos3b above, js engines try to calculate the value types before usage, which is much easier to do, when they don’t change the type during their lifetime. This allows js engines to apply additional performance optimizations.
If I remember something else, I’ll add it here.
In JS possible to write super performant code, but the readability of the code will be much-much worse than ReScript code with very similar performance.