svelte
svelte copied to clipboard
Unnecessarily calling `$.stringify` on strings
Describe the bug
$.stringify is called on all template values whether it needs to be or not. This results in larger output and slower performance.
Reproduction
https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAA0WM0QoCIRBFf0WGYAuEerYt6Duyh0VnSXBHccYixH8PH6LHc87lNlhDRAZzb0DLhmDgljNokE8ewC-MgqCBUy1umJldCVmulqxEFOVSJVEXtWNZBPenwy8I8vDTE2NM6p1K9NPZ0nz8H1Abo24JNGzJhzWgByOlYn_0L7HGqhKbAAAA
Logs
No response
System Info
`5.0.0-next.27`
Severity
annoyance
What's the expected outcome? Do static analysis to see if this is a static variable that can never change?
I think we've already done that. We've got binding.mutated and binding.reassigned so I think it'd be a pretty easy check to add
Actually template strings already do string coercion, so I wonder if we even need the $.stringify in other cases
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#string_coercion
stringify turns undefined and null into empty strings, so we can't rely on string coercion only
But I guess we could skip it in any cases where we know the variable cannot be null or undefined?
Couldn't we just use nullish coalescing?
$.stringify could be replaced with just doing something like
`${value??''}`
This issue seems to be outdated, should it be closed?