AgentSkillsCN

performance-optimization

优化内存、CPU、网络和渲染的性能。在提升 FPS、减少内存占用或修复性能瓶颈时使用。

SKILL.md
--- frontmatter
name: performance-optimization
description: Optimize performance for memory, CPU, network, and rendering. Use when improving FPS, reducing memory usage, or fixing performance bottlenecks.

⚡ Performance Optimization Skill

Memory Optimization

Avoid Memory Leaks

javascript
// ❌ Bad - listener not removed
element.addEventListener('click', handler);

// ✅ Good - cleanup
const controller = new AbortController();
element.addEventListener('click', handler, { signal: controller.signal });
// Later: controller.abort();

Object Pooling

javascript
class FramePool {
  constructor(size) {
    this.pool = Array(size).fill(null).map(() => new ArrayBuffer(1024 * 1024));
    this.available = [...this.pool];
  }
  
  acquire() {
    return this.available.pop() || new ArrayBuffer(1024 * 1024);
  }
  
  release(buffer) {
    this.available.push(buffer);
  }
}

Network Optimization

Debounce Requests

javascript
function debounce(fn, delay) {
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => fn(...args), delay);
  };
}

const debouncedSearch = debounce(searchAPI, 300);

Batch Requests

javascript
class RequestBatcher {
  constructor(batchFn, delay = 50) {
    this.queue = [];
    this.batchFn = batchFn;
    this.delay = delay;
  }
  
  add(item) {
    this.queue.push(item);
    clearTimeout(this.timer);
    this.timer = setTimeout(() => this.flush(), this.delay);
  }
  
  flush() {
    if (this.queue.length > 0) {
      this.batchFn([...this.queue]);
      this.queue = [];
    }
  }
}

Rendering Optimization

RequestAnimationFrame

javascript
// ❌ Bad
setInterval(render, 16);

// ✅ Good
function animate() {
  render();
  requestAnimationFrame(animate);
}
requestAnimationFrame(animate);

Throttle FPS

javascript
let lastFrame = 0;
const targetFPS = 30;
const frameInterval = 1000 / targetFPS;

function animate(timestamp) {
  if (timestamp - lastFrame >= frameInterval) {
    render();
    lastFrame = timestamp;
  }
  requestAnimationFrame(animate);
}

Image/Video Optimization

Compression

javascript
// Canvas to JPEG with quality
canvas.toBlob((blob) => {
  sendFrame(blob);
}, 'image/jpeg', 0.5); // 50% quality

// Resize before send
const scale = 0.5;
ctx.drawImage(video, 0, 0, width * scale, height * scale);

Lazy Loading

html
<img loading="lazy" src="image.jpg">

Profiling Tools

ToolUse Case
Chrome DevTools → PerformanceCPU, rendering
Chrome DevTools → MemoryMemory leaks
LighthouseOverall score
console.time()Measure code blocks
javascript
console.time('upload');
await uploadFile(file);
console.timeEnd('upload'); // upload: 1234ms

TitanMirror Performance

SettingValueImpact
Resolution480p-60% bandwidth
JPEG Quality50%-50% size
Target FPS25Smooth enough
ImageReader Buffer5Prevent drops