Skip to content

Eliminate unnecessary if statements #10049

Open
@benmccann

Description

@benmccann

Describe the problem

Consider the following code:

{#if iconData}
	{#if iconData.paths}
		{#each iconData.paths as path}
			<path {...path} />
		{/each}
	{/if}
{/if}

It could be rewritten as:

{#each iconData?.paths || [] as path}
	<path {...path} />
{/each}

This will save a larger amount of code than what is written in the source because the Svelte compiler must generate about ten lines for each if statement. At the very least it shouldn't be too hard to combine consecutive if statements into a single if statement

This example is from a real project: RobBrazier/svelte-awesome#1058

Describe the proposed solution

Implement it as an optimization in the compiler

Alternatives considered

Document it (possibly at https://kit.svelte.dev/docs/performance) and make users handle it themselves

Importance

nice to have

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions