Skip to content

Deduplicate if (detaching) checks for components with multiple root elements #6684

Closed
@K4rakara

Description

@K4rakara

Describe the bug

When creating a component with multiple root elements, Svelte currently checks detaching for every single root element. IE:

// Truncated for brevity, see REPL

// ...

function create_fragment(ctx) {
  // ...
  return {
    // ...
    d(detaching) {
      if (detaching) detach(h1);
      if (detaching) detach(t1);
      if (detaching) detach(h2);
      if (detaching) detach(t3);
      if (detaching) detach(h3);
    }
  };
}

// ...

When ran through Terser, that roughly becomes:

detaching && detach(h1),
detaching && detach(t1),
detaching && detach(h2),
detaching && detach(t2),
detaching && detach(h3);

This should be fairly simple to cut down to this:

if (detaching) {
  detach(h1);
  detach(t1);
  detach(h2);
  detach(t3);
  detach(h3);
}

... which roughly becomes this:

detaching && (
  detach(h1),
  detach(t1),
  detach(h2),
  detach(t3),
  detach(h3)
);

This is a really minor thing, but it should lead to slightly better bundle sizes for some users.

I'd be happy to fix this as a first pull request 😄

Reproduction

REPL

Logs

No response

System Info

I encountered this on the playground, so I suppose 3.42.3?

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions