Index of every page on this site: llms.txt
All skills
impertio-studio avatar

/threejs-errors-performance

@6c190f0
Run it yourself

Use when a Three.js scene has performance problems: low FPS, memory leaks, too many draw calls, or high GPU memory usage. Prevents the common mistake of forgetting dispose(), creating objects in the render loop, or not using InstancedMesh for repeated objects. Covers disposal patterns, draw call optimization, InstancedMesh, LOD, texture compression, renderer.info, Stats.js profiling. Keywords: performance, memory leak, dispose, draw calls, FPS, slow, optimization, InstancedMesh, LOD, renderer.info, Stats, profiling, laggy, low FPS, stuttering, browser freezes.

Use this Skill: https://skilld.dev/gh/impertio-studio/three.js-claude-skill-package/threejs-errors-performance

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesanti-patterns.md

≈1.9k tokens on demand. Your agent reads this file only when SKILL.md points to it.

threejs-errors-performance — Anti-Patterns

Anti-Pattern 1: Creating Objects in the Render Loop

WRONG:

function animate() {
  // LEAK: new geometry and material every frame, never disposed
  const geo = new THREE.BoxGeometry(1, 1, 1);
  const mat = new THREE.MeshStandardMaterial({ color: 0xff0000 });
  const mesh = new THREE.Mesh(geo, mat);
  scene.add(mesh);
  renderer.render(scene, camera);
  requestAnimationFrame(animate);
}

Why it fails: Each frame allocates new GPU buffers for geometry, compiles a new shader program for the material, and adds a new mesh to the scene. GPU memory grows without bound. The scene accumulates thousands of overlapping objects.

CORRECT:

const geo = new THREE.BoxGeometry(1, 1, 1);
const mat = new THREE.MeshStandardMaterial({ color: 0xff0000 });
const mesh = new THREE.Mesh(geo, mat);
scene.add(mesh);

function animate() {
  renderer.render(scene, camera);
  requestAnimationFrame(animate);
}

Anti-Pattern 2: Forgetting to Dispose Textures on Replacement

WRONG:

// Old texture stays in GPU memory forever
material.map = newTexture;
material.needsUpdate = true;

Why it fails: The old texture's GPU memory is NEVER freed. Over time, swapping textures without disposal accumulates hundreds of MB of leaked GPU memory. renderer.info.memory.textures will grow continuously.

CORRECT:

if (material.map) material.map.dispose();
material.map = newTexture;
material.needsUpdate = true;

Anti-Pattern 3: Individual Meshes Instead of InstancedMesh

WRONG:

// 5000 draw calls — GPU cannot batch these
for (let i = 0; i < 5000; i++) {
  const mesh = new THREE.Mesh(geometry.clone(), material.clone());
  mesh.position.set(Math.random() * 100, 0, Math.random() * 100);
  scene.add(mesh);
}

Why it fails: Each Mesh produces a separate draw call. The GPU driver overhead for 5000 draw calls dominates frame time. Additionally, cloning geometry and material 5000 times wastes memory — identical copies occupy GPU memory 5000 times.

CORRECT:

const mesh = new THREE.InstancedMesh(geometry, material, 5000);
const dummy = new THREE.Object3D();
for (let i = 0; i < 5000; i++) {
  dummy.position.set(Math.random() * 100, 0, Math.random() * 100);
  dummy.updateMatrix();
  mesh.setMatrixAt(i, dummy.matrix);
}
mesh.instanceMatrix.needsUpdate = true;
scene.add(mesh); // 1 draw call

Anti-Pattern 4: Allocating Temporary Objects in the Animation Loop

WRONG:

function animate() {
  // Creates garbage every frame — triggers frequent GC pauses
  const direction = new THREE.Vector3(1, 0, 0);
  const worldPos = new THREE.Vector3();
  mesh.getWorldPosition(worldPos);
  const distance = worldPos.distanceTo(new THREE.Vector3(0, 0, 0));
  requestAnimationFrame(animate);
}

Why it fails: JavaScript garbage collection pauses cause visible frame drops (stuttering). Creating Vector3, Matrix4, Quaternion, Color, or Box3 objects every frame generates significant GC pressure at 60 FPS (60 allocations/second per object).

CORRECT:

const _direction = new THREE.Vector3();
const _worldPos = new THREE.Vector3();
const _origin = new THREE.Vector3(0, 0, 0);

function animate() {
  _direction.set(1, 0, 0);
  mesh.getWorldPosition(_worldPos);
  const distance = _worldPos.distanceTo(_origin);
  requestAnimationFrame(animate);
}

Anti-Pattern 5: Not Disposing Controls on Cleanup

WRONG:

function destroyViewer() {
  scene.clear();
  renderer.dispose();
  renderer.domElement.remove();
  // controls.dispose() is missing!
}

Why it fails: OrbitControls, MapControls, FlyControls, and all other control classes attach event listeners to the DOM (mousedown, wheel, touchstart, pointermove, keydown, etc.). Without controls.dispose(), these listeners persist after the renderer is destroyed, causing errors when they try to interact with a disposed renderer and leaking memory via closures.

CORRECT:

function destroyViewer() {
  controls.dispose(); // ALWAYS dispose controls first
  scene.traverse((object) => {
    if (object.geometry) object.geometry.dispose();
    if (object.material) {
      if (Array.isArray(object.material)) {
        object.material.forEach(disposeMaterial);
      } else {
        disposeMaterial(object.material);
      }
    }
  });
  scene.clear();
  renderer.dispose();
  renderer.domElement.remove();
}

Anti-Pattern 6: Using MeshPhysicalMaterial Everywhere

WRONG:

// Compiles a massive shader for every object, even simple walls
const wallMaterial = new THREE.MeshPhysicalMaterial({ color: 0xcccccc });
const floorMaterial = new THREE.MeshPhysicalMaterial({ color: 0x888888 });

Why it fails: MeshPhysicalMaterial compiles a significantly larger shader than MeshStandardMaterial because it includes code paths for clearcoat, transmission, sheen, iridescence, and anisotropy — even when those features are not used. This wastes GPU cycles on every fragment.

CORRECT:

// Use MeshStandardMaterial for objects that do not need physical features
const wallMaterial = new THREE.MeshStandardMaterial({ color: 0xcccccc });
const floorMaterial = new THREE.MeshStandardMaterial({ color: 0x888888 });

// ONLY use MeshPhysicalMaterial when you need its unique features
const glassMaterial = new THREE.MeshPhysicalMaterial({
  transmission: 1.0,
  roughness: 0.0,
  ior: 1.5
});

Anti-Pattern 7: Forgetting instanceMatrix.needsUpdate

WRONG:

const mesh = new THREE.InstancedMesh(geo, mat, 100);
const dummy = new THREE.Object3D();

for (let i = 0; i < 100; i++) {
  dummy.position.set(i * 2, 0, 0);
  dummy.updateMatrix();
  mesh.setMatrixAt(i, dummy.matrix);
}
// Missing: mesh.instanceMatrix.needsUpdate = true;
scene.add(mesh);
// Result: all 100 instances render at (0, 0, 0) stacked on top of each other

Why it fails: setMatrixAt writes to the CPU-side typed array but does NOT trigger GPU upload. The GPU buffer still contains the initial identity matrices (all zeros = origin). Without needsUpdate = true, the data NEVER reaches the GPU.

CORRECT:

for (let i = 0; i < 100; i++) {
  dummy.position.set(i * 2, 0, 0);
  dummy.updateMatrix();
  mesh.setMatrixAt(i, dummy.matrix);
}
mesh.instanceMatrix.needsUpdate = true; // triggers GPU upload
scene.add(mesh);

Anti-Pattern 8: Disposing Shared Resources Too Early

WRONG:

const sharedGeometry = new THREE.BoxGeometry(1, 1, 1);
const sharedMaterial = new THREE.MeshStandardMaterial({ color: 0x00ff00 });

const meshA = new THREE.Mesh(sharedGeometry, sharedMaterial);
const meshB = new THREE.Mesh(sharedGeometry, sharedMaterial);
scene.add(meshA, meshB);

// Later, remove only meshA
scene.remove(meshA);
sharedGeometry.dispose(); // BUG: meshB still uses this geometry!
sharedMaterial.dispose();  // BUG: meshB still uses this material!
// meshB now renders as broken/invisible

Why it fails: dispose() frees GPU resources immediately. All meshes referencing the disposed geometry or material will fail to render. There is no reference counting in Three.js — you MUST track shared resources manually.

CORRECT:

scene.remove(meshA);
// Do NOT dispose shared resources until ALL consumers are removed
// Only dispose when meshB is also removed:
scene.remove(meshB);
sharedGeometry.dispose();
sharedMaterial.dispose();

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts5mo3 checks · Risk SAFE
  • Gen Agent Trust Hub5mo

    The skill is a comprehensive technical guide for optimizing Three.js performance. It includes best practices for memory management, draw call optimization, and GPU resource disposal. No security risks, malicious code, or suspicious patterns were detected.

  • Socket5mo

    No alerts

  • Snyk5mo

    Risk: LOW · No issues

Signed by skilld at 6c190f0. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Steadyupdated 8 months ago
compatibility
Designed for Claude Code. Requires Three.js r160+.
metadata
{
  "author": "OpenAEC-Foundation",
  "version": "1.0"
}

README badge

README badge for impertio-studio/three.js-claude-skill-package/threejs-errors-performance