This skill identifies performance bottlenecks and provides optimization suggestions.
Overview
It analyzes code execution time, memory usage, and algorithm complexity, and provides concrete optimization suggestions.
Main Features
- Algorithm Complexity Analysis: Evaluation using Big O notation
- N+1 Query Detection: Database query optimization
- Memory Leak Detection: Unreleased resources, circular references
- Caching Opportunities: Memoization, CDN utilization
- Asynchronous Processing: Parallelization, Promise optimization
- Bundle size: Tree shaking、Code splitting
- Rendering Optimization: Virtualization, lazy loading
Usage
このコードのパフォーマンス分析:
[コード]
分析項目:
- アルゴリズム複雑度
- メモリ使用量
- 最適化提案
Analysis Examples
N+1 Query Problem
Problematic Code:
// O(n) のクエリを n 回実行 = O(n²)
const posts = await Post.findAll();
for (const post of posts) {
post.author = await User.findById(post.authorId); // N+1問題
}
Optimization:
// O(n) + O(m) = O(n)
const posts = await Post.findAll();
const authorIds = posts.map(p => p.authorId);
const authors = await User.findByIds(authorIds); // 1回のクエリ
const authorMap = new Map(authors.map(a => [a.id, a]));
posts.forEach(post => post.author = authorMap.get(post.authorId));
Improvement: Query count 101 → 2, response time reduced by 90%
Algorithm Optimization
Inefficient:
# O(n²) - 遅い
def has_duplicates(arr):
for i in range(len(arr)):
for j in range(i + 1, len(arr)):
if arr[i] == arr[j]:
return True
return False
Optimization:
# O(n) - 高速
def has_duplicates(arr):
return len(arr) != len(set(arr))
Memory Optimization
Problem:
// メモリリーク: イベントリスナーが解放されない
component.addEventListener('click', handler);
Fix:
// クリーンアップ
const controller = new AbortController();
component.addEventListener('click', handler, { signal: controller.signal });
// コンポーネント破棄時
controller.abort();
Output Report
# パフォーマンス分析レポート
## サマリー
- **Critical**: 2件(即時対応必須)
- **High**: 4件(短期対応)
- **Medium**: 6件(中期改善)
## Critical 問題
### [CRITICAL] N+1 クエリ問題
**場所**: api/posts.ts:45-52
**影響**: 100件のデータで101回のクエリ実行
**レスポンス時間**: 2.5秒 → 0.3秒(88%改善可能)
### [CRITICAL] O(n²) アルゴリズム
**場所**: utils/search.py:23
**影響**: 10,000件で100,000,000回の比較
**実行時間**: 45秒 → 0.5秒(98%改善可能)
## 最適化提案
1. **データベースクエリ**: Eager loading使用
2. **アルゴリズム**: ハッシュテーブル活用
3. **キャッシング**: Redis導入
4. **非同期処理**: Promise.all で並列化
Best practices
- Measurement: Use profiling tools
- Bottleneck Identification: Optimize from the most impactful parts
- Trade-offs: Balance with readability
- Continuous Monitoring: Introduce APM tools
Version Information
- Skill version: 1.0.0
- Last updated: 2025-01-22