Optimizing Performance
This page will outline a few scenarios where you may be able to optimize performance of your application. Svelte and Pixi are very performant so you won’t have to think about it too much but they still have their limits!
Rendering Lots of Components
You may find yourself needing to render and update many components at once. Take the app on the homepage, for example. If we were to recreate this, we might imagine every Star as a <Sprite />
component that receives props every frame.
Notice how the performance sharply drops as you increase the amount of stars with the slider. Not great is it? There are a few reasons why:
-
Updating props for thousands of components is slow. Even if these were HTML elements, it would be just as bad. At this scale it’s always best to mutate the underlying Pixi instances (in this case, the sprites).
-
Mounting/unmounting thousands of components is slow. If you pay close attention you’ll notice a significant stutter when moving the slider up/down at the higher values.
Let’s take another approach by using Pixi a bit more directly:
Performance is much better now and there’s hardly any stutter when adding/removing stars.
If you wanted to squeeze out a bit more you could use a ParticleContainer instead of a regular Container
.
Render on Demand
Pixi applications typically render at 60 frames per second (or higher if the user’s screen has a higher refresh rate). This is perfectly fine and most WebGL apps function this way, but it could be wasteful to keep rendering if everything in your scene has stopped moving or animating. In which case it would be better to only render when our components have actually updated (e.g. after user interaction).
You can set render="demand"
on the Application
component to opt into this behaviour:
If you are using the Renderer
component, see this example.
Triggering Renders Manually
Every SveltePixi component will automatically trigger renders when they are updated when rendering on demand. If you are mutating Pixi instances directly or adding functionality to a base component you will need to mark that an update is required manually.