by throw21111972 on 6/27/24, 1:47 PM with 6 comments
by gizmo on 7/1/24, 10:26 PM
This is not true in anything except for toy interpreters, for a number of reasons.
One, internally strings are stored alongside the length. For 'TOP' and 'BOTTOM' the lengths don't match which means the strings can't be equal. That means inequality testing requires only a single integer comparison.
Two, strcmp is used for ordering strings, that's why it returns -1, 0, 1. That additional work isn't needed for testing equality. JS engines are not going to call strcmp unnecessarily.
Three, modern Javascript engines don't treat all strings the same. There are fast paths for ASCII strings, for interned strings, for concatenated strings, for strings that are slices of other strings, and more. From the perspective of the programmer a Javascript string is just a string. In reality a string in Javascript is nothing like C's char*.
I suspect -- but don't actually know -- that the string compare in the benchmark of example 1 is slower because the JS engine has to do a type check on the internal string representation. That's an extra branch the integer path doesn't need.
by seabass on 7/2/24, 2:21 AM
With that in mind, it’d be interesting to see the relative costs of each deoptimization to the others. For example, is a monomorphic->polymorphic deopt an order of magnitude worse than an unnecessary string mutation? In nanoseconds, what’s the actual cost of each of these? Because there are certainly going to be some optimizations that matter much more than others.
by emmanueloga_ on 7/2/24, 4:57 AM
Looking into how to get more debugging information out of a JS engine, I found this V8 blogpost that explains how to install a debugging version of V8 called "D8" [1].
I wish getting debug info was way easier though. Imagine being able to do something like this:
function xyz() { "use debug"; }
--by hsshhshshjk on 7/1/24, 10:17 PM