Open Rich-Harris opened 1 month ago
I may be saying something stupid, but why not also replace the reactive $.get(name) ?? ""
by something like $.txt(name)
?
With txt()
defined like this :
function txt(signal) {
return get(signal) ?? "";
}
Because then you're adding another function call, which is more expensive. And not everything is of the $.get(foo)
form, sometimes it's just foo
or foo()
or whatever
Another stupid idea : why not using a tagged template for cleaning null/undefined
-$.template_effect(() => $.set_text(text, `${i ?? ""}: ${l ?? ""}`));
+$.template_effect(() => $.set_text(text, $.TXT`${i}: ${l}`));
With TXT
defined like that :
function TXT(strings, ...args) {
for (let i = args.length-1; i>=0; i--) {
args[i] ??= '';
}
return String.raw(strings, ...args);
}
The same reason — you're adding a function call which will make everything slower.
Describe the problem
There are many cases where knowing what values a binding could potentially hold would allow us to generate more optimal code. For example, in a situation like this...
...we don't need the
?? ""
in the generated code, because we can determine thatname
is always defined:The same goes for references to an index (and, if we were sophisticated enough, the item itself) inside an each block:
Post-#13264, it would also allow us to avoid creating effects in cases where we know an expression doesn't contain state. Right now, code like this...
...yields output like this...
...but as of #13264 yields this unnecessary overhead:
Describe the proposed solution
Track all the assignments (along with the scopes in which they occur) when first analysing bindings, so that we can later determine things like:
?? ""
)There are limits to what we can know with such an analysis — often, the answer to 'what could this value be?' is
:shrug:
. It's no substitute for knowing the entirety of your program (though we could one day augment it with knowledge of prop values and imports, for example), and nor is it a replacement for an type system (though unlike types, code can't lie about the range of possible values). But it's a relatively simple and cheap way to get a decent chunk of the benefits of a more comprehensive system.Importance
nice to have