svelte icon indicating copy to clipboard operation
svelte copied to clipboard

Unnecessarily calling `$.stringify` on strings

Open benmccann opened this issue 1 year ago • 6 comments

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

benmccann avatar Dec 29 '23 23:12 benmccann

What's the expected outcome? Do static analysis to see if this is a static variable that can never change?

dummdidumm avatar Dec 30 '23 09:12 dummdidumm

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

benmccann avatar Dec 30 '23 13:12 benmccann

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

benmccann avatar Dec 30 '23 14:12 benmccann

stringify turns undefined and null into empty strings, so we can't rely on string coercion only

gtm-nayan avatar Dec 30 '23 14:12 gtm-nayan

But I guess we could skip it in any cases where we know the variable cannot be null or undefined?

benmccann avatar Dec 30 '23 14:12 benmccann

Couldn't we just use nullish coalescing? $.stringify could be replaced with just doing something like

`${value??''}`

Ocean-OS avatar Sep 14 '24 20:09 Ocean-OS

This issue seems to be outdated, should it be closed?

Ocean-OS avatar May 02 '25 00:05 Ocean-OS