Frontory

Open code, practice, and notes →

Download source with English comments · for reading

Canvas 2D · 25 Particle Reuse and Allocation

Series contents · Technology map

Stage: Accessibility, Particles, and Performance Verification · Editable example · Storage ID: canvas-2d-step-25

Goals and learning outcomes

Prerequisites

Prerequisite concepts: Particle Lifetime, Emission Rate. Prepare the result files and observation records from Unit 24.

Key concepts and explanations

Object Reuse

Creating new objects every frame may increase garbage collection work. A pool reuses slots from dead particles.

Separating Measurements

Fewer allocations do not guarantee better frame times. If drawing dominates the cost, total time may change little.

Practice setup

First check the execution scope guidance on the example screen. The following describes preparation for extending the assignment in a separate project. Implement this yourself in an HTML and ES module project on a separate local HTTP development server. Record the browser version, viewport, and DPR.

The linked example is a starting point. It does not implement every task below. Complete work beyond its supported scope in a separate project.

Practice tasks

  1. Prepare day-25.js and index.html in the external practice project. Implement particle creation and return with a fixed-size pool and active flags.
  2. Compare frame records for array allocation and pooling with the same seed and emission rate.
  3. Save the output with the input condition new object for every emission, then run again with reuse inactive slots. Expected: Allocations decrease, but the degree of frame improvement must be measured. Compare this with the actual screen and logs, and record the actual results and reasons for differences in day-25-notes.md.

Variables to observe

VariableBaselineChangeExpected change / hypothesis
Allocation Policynew object for every emissionreuse inactive slotsAllocations decrease, but the degree of frame improvement must be measured

Predictions in this table are not measured results. Change one condition at a time and record why the actual result differs.

Completion criteria

Self-check

  1. What changes if you grow the pool indefinitely?
  2. How do you compare the costs of compacting arrays and iterating inactive slots?

Official references

Linked example and execution scope

Example source · Runtime environments and limits

This is an editable browser experiment. Measurements vary with the current device and browser.

Source code and records are not overwritten automatically. Resetting an existing example also preserves your notes.

Next learning

Previous: Particle State and Emission · Next: Frame Measurement and Bottlenecks

Open code, practice, and notes →