Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
Shall I create these functions in parent component and pass them to child for a performance improvement? Or is Svelte able to "hoist" them?

Let's say I have a component:

  • Page.svelte:
<script lang="ts">
  const players: Player[] = getPlayersFromSomewhere()
</script>

{#each players || [] as player}
  <Player {player} />
{/each}
  • and Player.svelte:
<script lang="ts">
  export let player: Player;
  
    async function doSomethingWithPlayer() {
    // many lines of code here...
    }

    async function doSomethingElse() {
    // many lines of code here...
    }

    async function doSomethingElseAgain() {
    // many lines of code here...
    }

  // ... many functions here
</script>

player is used here with many buttons like:

<button on:click={doSomethingWithPlayer}>...</button>
<button on:click={doSomethingElse}>...</button>
<button on:click={doSomethingElseAgain}>...</button>

THE QUESTION IS

Is Svelte recreating the functions in Player.svelte for each player or just one time using it for each player?

Shall I create the functions in Page.svelte and pass them to Player.svelte? Or is Svelte able to "hoist" them?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Svelte will automatically hoist functions that don't depend on local component state. From the docs:

Don't worry about the fact that we're redeclaring the foo function for every component instance — Svelte will hoist any functions that don't depend on local state out of the component definition.

You can see this behavior in the Svelte REPL. Consider the following component.

<script>
    let name = 'world';
    
    function changeName() {
        name = name + 1;
    }
    
    function log() {
        console.log("I'm independent")
    }
</script>

<h1>Hello {name}!</h1>

<button on:click={changeName}>
    Change
</button>

<button on:click={log}>
    Log
</button>

changeName depends on component state, but log does not, so it is hoisted out of the component. You see the following in the compiled JS: there is only one log function, but changeName is created per instance.

function log() {
    console.log("I'm independent");
}

function instance($$self, $$props, $$invalidate) {
    let name = 'world';

    function changeName() {
        $$invalidate(0, name = name + 1);
    }

    return [name, changeName];
}

class App extends SvelteComponent {
    constructor(options) {
        super();
        init(this, options, instance, create_fragment, safe_not_equal, {});
    }
}
about 4 years ago · Juan Pablo Isaza Report

0

You could use the module context to run code only once regardless of how many times you initialize the component. I recommend you look at this answer from Rich Harris and the official module context tutorial.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!