2 ms·
Question about the first answer, is there anywhere in the spec that demands this behavior? If the JS interpreter silently 'optimized' the branch into var a
by Spivak 9y ago
Question about the first answer, is there anywhere in the spec that demands this behavior? If the JS interpreter silently 'optimized' the branch into
var a_ = a.toString();
if (a_ === "1" && a_ === "2" && a_ === "3") {
...
or really just
if(false) {
...
would it be in violation?
- tlb 9y agoI believe so, if 'a' resolves to a getter. To optimize, it would have to prove that 'a' is a locally bound variable with no escapes or else no functions are called in the comparison. See https://v8project.blogspot.com/2017/09/disabling-escape-analysis.html https://v8project.blogspot.com/2017/09/disabling-escape-anal... for a related bug.
- TheRealPomax 9y agoIt would certainly be a violation, as the algorithm to evaluate == is rigorously outlined in the language specification, specifically here: https://www.ecma-international.org/ecma-262/8.0/index.html#sec-testing-and-comparison-operations https://www.ecma-international.org/ecma-262/8.0/index.html#s... This algorithm does not describe an optional caching step to speed up/short-circuit the procedure, and so the optimization you describe would lead to non-spec-compliant behaviour.