How Matter.js Iterations Affect Performance

In Matter.js, iterations determine how many times the physics engine calculates collision resolution and constraint solving per frame. Increasing iteration settings improves physical accuracy, stabilizes rigid body stacking, and reduces visual artifacts like jitter and tunneling. However, because each iteration adds repetitive mathematical calculations across all active bodies, scaling these values up directly increases CPU load and can cause severe frame drops if not balanced properly.

How Iterations Work in Matter.js

Matter.js relies on an iterative impulse solver. During each engine update, bodies that collide or are bound by constraints undergo multiple passes to converge toward a realistic physical state. The engine provides three primary configuration properties:

The Benefits of Increasing Iterations

Raising these values provides three main physical improvements:

  1. Stiffer and More Stable Stacks: Low iterations cause stacks of bodies to drift, oscillate, or compress artificially under gravity. Higher position and velocity iterations allow the engine to distribute normal forces correctly, keeping stacked boxes or shapes rigid.
  2. Reduced Object Tunneling: When objects move rapidly, low position iterations can allow bodies to clip into or pass through one another. More solver passes catch and resolve overlaps before bodies permanently penetrate boundaries.
  3. Rigid Constraints: Constraints (such as springs, ropes, or joints) can feel rubbery or stretch beyond their defined length under stress. Higher constraint iterations maintain accurate distances without unwanted elasticity.

The Performance Cost

Physics calculation in a game loop must complete well within the target frame budget (typically under 16.6 milliseconds for 60 FPS). Increasing iteration counts creates the following performance bottlenecks:

Best Practices for Optimization

To maintain steady frame rates while achieving the necessary physical realism, use these optimization strategies: