@martinffx/atelier 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +1 -5
- package/package.json +1 -1
- package/skills/{code-conventional-commit → code-commit}/SKILL.md +16 -1
- package/skills/code-commit/references/pr-workflow.md +145 -0
- package/skills/code-debug/SKILL.md +0 -4
- package/skills/code-review/references/reviewers.md +3 -11
- package/skills/code-review/references/rq.md +4 -5
- package/skills/oracle-grillme/SKILL.md +160 -0
- package/skills/oracle-grillme/references/ADR-FORMAT.md +47 -0
- package/skills/oracle-grillme/references/CONTEXT-FORMAT.md +63 -0
- package/skills/spec-finish/SKILL.md +2 -5
- package/skills/spec-implement/SKILL.md +1 -1
- package/skills/code-conventional-commit/evals/evals.json +0 -23
- package/skills/code-conventional-commit/workspace/iteration-1/eval-0/with_skill/outputs/commit-message.txt +0 -4
- package/skills/code-conventional-commit/workspace/iteration-1/eval-0/with_skill/outputs/eval_metadata.json +0 -6
- package/skills/code-conventional-commit/workspace/iteration-1/eval-1/with_skill/outputs/eval_metadata.json +0 -6
- package/skills/code-conventional-commit/workspace/iteration-1/eval-1/with_skill/outputs/validation-result.txt +0 -16
- package/skills/code-conventional-commit/workspace/iteration-1/eval-2/with_skill/outputs/commit-message.txt +0 -8
- package/skills/code-conventional-commit/workspace/iteration-1/eval-2/with_skill/outputs/eval_metadata.json +0 -6
- package/skills/code-conventional-commit/workspace/iteration-1/report.html +0 -1325
- package/skills/code-git-worktrees/SKILL.md +0 -218
- package/skills/code-perf/SKILL.md +0 -163
- package/skills/code-perf/references/common-bottlenecks.md +0 -124
- package/skills/code-perf/references/optimization-patterns.md +0 -167
- package/skills/code-perf/references/profiling-tools.md +0 -126
- package/skills/code-security/SKILL.md +0 -183
- package/skills/code-security/references/owasp-top-10.md +0 -129
- package/skills/code-security/references/security-tools.md +0 -102
- package/skills/code-security/references/vulnerability-patterns.md +0 -129
- package/skills/code-stacked-commit/SKILL.md +0 -155
|
@@ -1,218 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: code-git-worktrees
|
|
3
|
-
description: Use when starting feature work that needs isolation from current workspace - creates isolated git worktrees with smart directory selection and safety verification. Triggers on worktree, git worktree, isolated workspace, or creating a new branch for feature work.
|
|
4
|
-
user-invocable: true
|
|
5
|
-
---
|
|
6
|
-
|
|
7
|
-
# Using Git Worktrees
|
|
8
|
-
|
|
9
|
-
## Overview
|
|
10
|
-
|
|
11
|
-
Git worktrees create isolated workspaces sharing the same repository, allowing work on multiple branches simultaneously without switching.
|
|
12
|
-
|
|
13
|
-
**Core principle:** Systematic directory selection + safety verification = reliable isolation.
|
|
14
|
-
|
|
15
|
-
**Announce at start:** "I'm using the using-git-worktrees skill to set up an isolated workspace."
|
|
16
|
-
|
|
17
|
-
## Directory Selection Process
|
|
18
|
-
|
|
19
|
-
Follow this priority order:
|
|
20
|
-
|
|
21
|
-
### 1. Check Existing Directories
|
|
22
|
-
|
|
23
|
-
```bash
|
|
24
|
-
# Check in priority order
|
|
25
|
-
ls -d .worktrees 2>/dev/null # Preferred (hidden)
|
|
26
|
-
ls -d worktrees 2>/dev/null # Alternative
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
**If found:** Use that directory. If both exist, `.worktrees` wins.
|
|
30
|
-
|
|
31
|
-
### 2. Check AGENTS.md
|
|
32
|
-
|
|
33
|
-
```bash
|
|
34
|
-
grep -i "worktree.*director" AGENTS.md 2>/dev/null
|
|
35
|
-
```
|
|
36
|
-
|
|
37
|
-
**If preference specified:** Use it without asking.
|
|
38
|
-
|
|
39
|
-
### 3. Ask User
|
|
40
|
-
|
|
41
|
-
If no directory exists and no AGENTS.md preference:
|
|
42
|
-
|
|
43
|
-
```
|
|
44
|
-
No worktree directory found. Where should I create worktrees?
|
|
45
|
-
|
|
46
|
-
1. .worktrees/ (project-local, hidden)
|
|
47
|
-
2. ~/.config/superpowers/worktrees/<project-name>/ (global location)
|
|
48
|
-
|
|
49
|
-
Which would you prefer?
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
## Safety Verification
|
|
53
|
-
|
|
54
|
-
### For Project-Local Directories (.worktrees or worktrees)
|
|
55
|
-
|
|
56
|
-
**MUST verify directory is ignored before creating worktree:**
|
|
57
|
-
|
|
58
|
-
```bash
|
|
59
|
-
# Check if directory is ignored (respects local, global, and system gitignore)
|
|
60
|
-
git check-ignore -q .worktrees 2>/dev/null || git check-ignore -q worktrees 2>/dev/null
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
**If NOT ignored:**
|
|
64
|
-
|
|
65
|
-
Per Jesse's rule "Fix broken things immediately":
|
|
66
|
-
1. Add appropriate line to .gitignore
|
|
67
|
-
2. Commit the change
|
|
68
|
-
3. Proceed with worktree creation
|
|
69
|
-
|
|
70
|
-
**Why critical:** Prevents accidentally committing worktree contents to repository.
|
|
71
|
-
|
|
72
|
-
### For Global Directory (~/.config/superpowers/worktrees)
|
|
73
|
-
|
|
74
|
-
No .gitignore verification needed - outside project entirely.
|
|
75
|
-
|
|
76
|
-
## Creation Steps
|
|
77
|
-
|
|
78
|
-
### 1. Detect Project Name
|
|
79
|
-
|
|
80
|
-
```bash
|
|
81
|
-
project=$(basename "$(git rev-parse --show-toplevel)")
|
|
82
|
-
```
|
|
83
|
-
|
|
84
|
-
### 2. Create Worktree
|
|
85
|
-
|
|
86
|
-
```bash
|
|
87
|
-
# Determine full path
|
|
88
|
-
case $LOCATION in
|
|
89
|
-
.worktrees|worktrees)
|
|
90
|
-
path="$LOCATION/$BRANCH_NAME"
|
|
91
|
-
;;
|
|
92
|
-
~/.config/superpowers/worktrees/*)
|
|
93
|
-
path="~/.config/superpowers/worktrees/$project/$BRANCH_NAME"
|
|
94
|
-
;;
|
|
95
|
-
esac
|
|
96
|
-
|
|
97
|
-
# Create worktree with new branch
|
|
98
|
-
git worktree add "$path" -b "$BRANCH_NAME"
|
|
99
|
-
cd "$path"
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
### 3. Run Project Setup
|
|
103
|
-
|
|
104
|
-
Auto-detect and run appropriate setup:
|
|
105
|
-
|
|
106
|
-
```bash
|
|
107
|
-
# Node.js
|
|
108
|
-
if [ -f package.json ]; then npm install; fi
|
|
109
|
-
|
|
110
|
-
# Rust
|
|
111
|
-
if [ -f Cargo.toml ]; then cargo build; fi
|
|
112
|
-
|
|
113
|
-
# Python
|
|
114
|
-
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
|
115
|
-
if [ -f pyproject.toml ]; then poetry install; fi
|
|
116
|
-
|
|
117
|
-
# Go
|
|
118
|
-
if [ -f go.mod ]; then go mod download; fi
|
|
119
|
-
```
|
|
120
|
-
|
|
121
|
-
### 4. Verify Clean Baseline
|
|
122
|
-
|
|
123
|
-
Run tests to ensure worktree starts clean:
|
|
124
|
-
|
|
125
|
-
```bash
|
|
126
|
-
# Examples - use project-appropriate command
|
|
127
|
-
npm test
|
|
128
|
-
cargo test
|
|
129
|
-
pytest
|
|
130
|
-
go test ./...
|
|
131
|
-
```
|
|
132
|
-
|
|
133
|
-
**If tests fail:** Report failures, ask whether to proceed or investigate.
|
|
134
|
-
|
|
135
|
-
**If tests pass:** Report ready.
|
|
136
|
-
|
|
137
|
-
### 5. Report Location
|
|
138
|
-
|
|
139
|
-
```
|
|
140
|
-
Worktree ready at <full-path>
|
|
141
|
-
Tests passing (<N> tests, 0 failures)
|
|
142
|
-
Ready to implement <feature-name>
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
## Quick Reference
|
|
146
|
-
|
|
147
|
-
| Situation | Action |
|
|
148
|
-
|-----------|--------|
|
|
149
|
-
| `.worktrees/` exists | Use it (verify ignored) |
|
|
150
|
-
| `worktrees/` exists | Use it (verify ignored) |
|
|
151
|
-
| Both exist | Use `.worktrees/` |
|
|
152
|
-
| Neither exists | Check AGENTS.md → Ask user |
|
|
153
|
-
| Directory not ignored | Add to .gitignore + commit |
|
|
154
|
-
| Tests fail during baseline | Report failures + ask |
|
|
155
|
-
| No package.json/Cargo.toml | Skip dependency install |
|
|
156
|
-
|
|
157
|
-
## Common Mistakes
|
|
158
|
-
|
|
159
|
-
### Skipping ignore verification
|
|
160
|
-
|
|
161
|
-
- **Problem:** Worktree contents get tracked, pollute git status
|
|
162
|
-
- **Fix:** Always use `git check-ignore` before creating project-local worktree
|
|
163
|
-
|
|
164
|
-
### Assuming directory location
|
|
165
|
-
|
|
166
|
-
- **Problem:** Creates inconsistency, violates project conventions
|
|
167
|
-
- **Fix:** Follow priority: existing > AGENTS.md > ask
|
|
168
|
-
|
|
169
|
-
### Proceeding with failing tests
|
|
170
|
-
|
|
171
|
-
- **Problem:** Can't distinguish new bugs from pre-existing issues
|
|
172
|
-
- **Fix:** Report failures, get explicit permission to proceed
|
|
173
|
-
|
|
174
|
-
### Hardcoding setup commands
|
|
175
|
-
|
|
176
|
-
- **Problem:** Breaks on projects using different tools
|
|
177
|
-
- **Fix:** Auto-detect from project files (package.json, etc.)
|
|
178
|
-
|
|
179
|
-
## Example Workflow
|
|
180
|
-
|
|
181
|
-
```
|
|
182
|
-
You: I'm using the using-git-worktrees skill to set up an isolated workspace.
|
|
183
|
-
|
|
184
|
-
[Check .worktrees/ - exists]
|
|
185
|
-
[Verify ignored - git check-ignore confirms .worktrees/ is ignored]
|
|
186
|
-
[Create worktree: git worktree add .worktrees/auth -b feature/auth]
|
|
187
|
-
[Run npm install]
|
|
188
|
-
[Run npm test - 47 passing]
|
|
189
|
-
|
|
190
|
-
Worktree ready at /Users/jesse/myproject/.worktrees/auth
|
|
191
|
-
Tests passing (47 tests, 0 failures)
|
|
192
|
-
Ready to implement auth feature
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
## Red Flags
|
|
196
|
-
|
|
197
|
-
**Never:**
|
|
198
|
-
- Create worktree without verifying it's ignored (project-local)
|
|
199
|
-
- Skip baseline test verification
|
|
200
|
-
- Proceed with failing tests without asking
|
|
201
|
-
- Assume directory location when ambiguous
|
|
202
|
-
- Skip AGENTS.md check
|
|
203
|
-
|
|
204
|
-
**Always:**
|
|
205
|
-
- Follow directory priority: existing > AGENTS.md > ask
|
|
206
|
-
- Verify directory is ignored for project-local
|
|
207
|
-
- Auto-detect and run project setup
|
|
208
|
-
- Verify clean test baseline
|
|
209
|
-
|
|
210
|
-
## Integration
|
|
211
|
-
|
|
212
|
-
**Called by:**
|
|
213
|
-
- Any skill needing isolated workspace for feature work
|
|
214
|
-
- When user explicitly requests worktree setup
|
|
215
|
-
- Before long-running implementation tasks that could pollute current workspace
|
|
216
|
-
|
|
217
|
-
**Pairs with:**
|
|
218
|
-
- `stacked-commit` - Use worktrees when managing stacked commits across multiple branches
|
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: code-perf
|
|
3
|
-
description: >
|
|
4
|
-
Performance profiling and optimization. Use when code is slow, has memory issues, or needs
|
|
5
|
-
optimization. Triggers on "slow", "performance", "optimize", "memory leak", "profiling",
|
|
6
|
-
"bottleneck", or any performance-related concern.
|
|
7
|
-
user-invocable: true
|
|
8
|
-
---
|
|
9
|
-
|
|
10
|
-
# Code Perf
|
|
11
|
-
|
|
12
|
-
Performance profiling and optimization workflow.
|
|
13
|
-
|
|
14
|
-
## The Golden Rule
|
|
15
|
-
|
|
16
|
-
**Never optimize without profiling.**
|
|
17
|
-
|
|
18
|
-
You don't know where the bottleneck is until you measure. Making changes without
|
|
19
|
-
profiling wastes time and often makes things worse.
|
|
20
|
-
|
|
21
|
-
---
|
|
22
|
-
|
|
23
|
-
## Workflow
|
|
24
|
-
|
|
25
|
-
### 1. Profile
|
|
26
|
-
|
|
27
|
-
Measure the current performance.
|
|
28
|
-
|
|
29
|
-
**Questions:**
|
|
30
|
-
- What's slow? (endpoint, operation, page load)
|
|
31
|
-
- How slow? (latency, throughput)
|
|
32
|
-
- Under what conditions? (load, data size)
|
|
33
|
-
|
|
34
|
-
**Tools:**
|
|
35
|
-
- Browser DevTools (Performance tab)
|
|
36
|
-
- Node clinic.js
|
|
37
|
-
- Python cProfile
|
|
38
|
-
- Go pprof
|
|
39
|
-
|
|
40
|
-
### 2. Identify
|
|
41
|
-
|
|
42
|
-
Find the hot paths.
|
|
43
|
-
|
|
44
|
-
**Look for:**
|
|
45
|
-
- Functions called many times
|
|
46
|
-
- Large memory allocations
|
|
47
|
-
- Blocking I/O
|
|
48
|
-
- Unnecessary computations
|
|
49
|
-
|
|
50
|
-
### 3. Optimize
|
|
51
|
-
|
|
52
|
-
Apply the right optimization pattern.
|
|
53
|
-
|
|
54
|
-
### 4. Verify
|
|
55
|
-
|
|
56
|
-
Re-profile to confirm improvement.
|
|
57
|
-
|
|
58
|
-
---
|
|
59
|
-
|
|
60
|
-
## Common Bottlenecks
|
|
61
|
-
|
|
62
|
-
See references/common-bottlenecks.md for detailed patterns:
|
|
63
|
-
|
|
64
|
-
| Pattern | Symptom | Solution |
|
|
65
|
-
|---------|---------|----------|
|
|
66
|
-
| N+1 | Multiple DB queries | Eager load, batch |
|
|
67
|
-
| Memory leak | Growing RSS | Clear caches, weak refs |
|
|
68
|
-
| Blocking I/O | Thread blocked | Async, worker pool |
|
|
69
|
-
| Unnecessary work | CPU high | Skip redundant calc |
|
|
70
|
-
| Large data | Slow serialization | Pagination, streams |
|
|
71
|
-
|
|
72
|
-
---
|
|
73
|
-
|
|
74
|
-
## Optimization Patterns
|
|
75
|
-
|
|
76
|
-
See references/optimization-patterns.md for:
|
|
77
|
-
|
|
78
|
-
### Caching
|
|
79
|
-
|
|
80
|
-
```javascript
|
|
81
|
-
// Before: every call hits DB
|
|
82
|
-
const user = await db.users.find(id);
|
|
83
|
-
|
|
84
|
-
// After: cache
|
|
85
|
-
const cacheKey = `user:${id}`;
|
|
86
|
-
let user = await redis.get(cacheKey);
|
|
87
|
-
if (!user) {
|
|
88
|
-
user = await db.users.find(id);
|
|
89
|
-
await redis.set(cacheKey, user, 'EX', 300);
|
|
90
|
-
}
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
### Lazy Loading
|
|
94
|
-
|
|
95
|
-
```javascript
|
|
96
|
-
// Before: load everything
|
|
97
|
-
const allUsers = await db.users.findMany();
|
|
98
|
-
|
|
99
|
-
// After: paginate
|
|
100
|
-
const users = await db.users.findMany({
|
|
101
|
-
take: 20,
|
|
102
|
-
skip: page * 20
|
|
103
|
-
});
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
### Connection Pooling
|
|
107
|
-
|
|
108
|
-
```javascript
|
|
109
|
-
// Before: new connection each time
|
|
110
|
-
const client = new Client();
|
|
111
|
-
await client.connect();
|
|
112
|
-
|
|
113
|
-
// After: pool
|
|
114
|
-
const pool = new Pool({ max: 20 });
|
|
115
|
-
// Use pool.query() throughout
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
---
|
|
119
|
-
|
|
120
|
-
## Profiling Tools
|
|
121
|
-
|
|
122
|
-
See references/profiling-tools.md for:
|
|
123
|
-
|
|
124
|
-
### Node.js
|
|
125
|
-
|
|
126
|
-
```bash
|
|
127
|
-
# CPU profiling
|
|
128
|
-
node --prof app.js
|
|
129
|
-
|
|
130
|
-
# Memory
|
|
131
|
-
node --inspect app.js # Chrome DevTools
|
|
132
|
-
|
|
133
|
-
# clinic.js
|
|
134
|
-
npx clinic doctor -- node app.js
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
### Python
|
|
138
|
-
|
|
139
|
-
```bash
|
|
140
|
-
# cProfile
|
|
141
|
-
python -m cProfile -o profile.prof app.py
|
|
142
|
-
|
|
143
|
-
# view with: python -m cProfile -s cumulative app.py
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
### Go
|
|
147
|
-
|
|
148
|
-
```bash
|
|
149
|
-
# pprof
|
|
150
|
-
go test -cpuprofile=cpu.prof
|
|
151
|
-
go tool pprof cpu.prof
|
|
152
|
-
|
|
153
|
-
# Web interface
|
|
154
|
-
go tool pprof -http=:8080 cpu.prof
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
---
|
|
158
|
-
|
|
159
|
-
## Skill Loading
|
|
160
|
-
|
|
161
|
-
- If optimization involves queries → load python-sqlalchemy or typescript-drizzle-orm
|
|
162
|
-
- If memory issues → load for heap snapshots
|
|
163
|
-
- If database → load appropriate database skill
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
# Common Performance Bottlenecks
|
|
2
|
-
|
|
3
|
-
## N+1 Query Problem
|
|
4
|
-
|
|
5
|
-
### Symptom
|
|
6
|
-
Many database queries when one should suffice.
|
|
7
|
-
|
|
8
|
-
### Diagnosis
|
|
9
|
-
```sql
|
|
10
|
-
-- Enable query logging
|
|
11
|
-
-- See N+1 queries in logs
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
### Solution
|
|
15
|
-
|
|
16
|
-
**Before:**
|
|
17
|
-
```python
|
|
18
|
-
for user in users:
|
|
19
|
-
print(user.profile.name) # Each = query
|
|
20
|
-
```
|
|
21
|
-
|
|
22
|
-
**After:**
|
|
23
|
-
```python
|
|
24
|
-
# Eager load
|
|
25
|
-
users = db.query(users).options(joinedload(User.profile)).all()
|
|
26
|
-
|
|
27
|
-
# Or batch
|
|
28
|
-
profiles = ProfileService.get_many([u.id for u in users])
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
---
|
|
32
|
-
|
|
33
|
-
## Memory Leaks
|
|
34
|
-
|
|
35
|
-
### Symptom
|
|
36
|
-
RSS grows over time, never回收es.
|
|
37
|
-
|
|
38
|
-
### Diagnosis
|
|
39
|
-
- Chrome DevTools Memory tab
|
|
40
|
-
- heap snapshots before/after
|
|
41
|
-
|
|
42
|
-
### Common Causes
|
|
43
|
-
|
|
44
|
-
1. **Unclosed connections**
|
|
45
|
-
2. **Growing caches** (no eviction)
|
|
46
|
-
3. **Event listeners** (not removed)
|
|
47
|
-
4. **Closures holding references**
|
|
48
|
-
5. **Global variables**
|
|
49
|
-
|
|
50
|
-
### Solution
|
|
51
|
-
```javascript
|
|
52
|
-
// Clear cache
|
|
53
|
-
cache.clear();
|
|
54
|
-
|
|
55
|
-
// Remove listener
|
|
56
|
-
element.removeEventListener('click', handler);
|
|
57
|
-
|
|
58
|
-
// Use weak refs
|
|
59
|
-
const ref = new WeakRef(obj);
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
---
|
|
63
|
-
|
|
64
|
-
## Blocking I/O
|
|
65
|
-
|
|
66
|
-
### Symptom
|
|
67
|
-
Thread/process blocked, can't handle other requests.
|
|
68
|
-
|
|
69
|
-
### Solution
|
|
70
|
-
|
|
71
|
-
```javascript
|
|
72
|
-
// Before: blocking
|
|
73
|
-
const data = fs.readFileSync('file.txt');
|
|
74
|
-
|
|
75
|
-
// After: async
|
|
76
|
-
const data = await fs.promises.readFile('file.txt');
|
|
77
|
-
```
|
|
78
|
-
|
|
79
|
-
---
|
|
80
|
-
|
|
81
|
-
## Unnecessary Work
|
|
82
|
-
|
|
83
|
-
### Symptom
|
|
84
|
-
CPU high, doing redundant computation.
|
|
85
|
-
|
|
86
|
-
### Examples
|
|
87
|
-
|
|
88
|
-
1. **Validation twice** - in frontend and backend
|
|
89
|
-
2. **Same calculation multiple times**
|
|
90
|
-
3. **Processing data not needed**
|
|
91
|
-
|
|
92
|
-
### Solution
|
|
93
|
-
```javascript
|
|
94
|
-
// Cache expensive computations
|
|
95
|
-
const memoize = fn => {
|
|
96
|
-
const cache = new Map();
|
|
97
|
-
return (...args) => {
|
|
98
|
-
const key = JSON.stringify(args);
|
|
99
|
-
return cache.get(key) ?? fn(...args);
|
|
100
|
-
};
|
|
101
|
-
};
|
|
102
|
-
```
|
|
103
|
-
|
|
104
|
-
---
|
|
105
|
-
|
|
106
|
-
## Large Data Serialization
|
|
107
|
-
|
|
108
|
-
### Symptom
|
|
109
|
-
Slow JSON serialization of large objects.
|
|
110
|
-
|
|
111
|
-
### Solution
|
|
112
|
-
|
|
113
|
-
```javascript
|
|
114
|
-
// Pagination
|
|
115
|
-
const page = req.query.page || 1;
|
|
116
|
-
const limit = 20;
|
|
117
|
-
const users = await db.users.findMany({
|
|
118
|
-
take: limit,
|
|
119
|
-
skip: (page - 1) * limit
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
// Streams for large files
|
|
123
|
-
stream.pipe(JSON.stringify());
|
|
124
|
-
```
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
# Optimization Patterns
|
|
2
|
-
|
|
3
|
-
## Caching
|
|
4
|
-
|
|
5
|
-
### When
|
|
6
|
-
- Same data requested multiple times
|
|
7
|
-
- Expensive computation
|
|
8
|
-
- External API calls
|
|
9
|
-
|
|
10
|
-
### Patterns
|
|
11
|
-
|
|
12
|
-
**In-memory:**
|
|
13
|
-
```javascript
|
|
14
|
-
const cache = new Map();
|
|
15
|
-
|
|
16
|
-
async function getUser(id) {
|
|
17
|
-
if (cache.has(id)) return cache.get(id);
|
|
18
|
-
const user = await db.find(id);
|
|
19
|
-
cache.set(id, user);
|
|
20
|
-
return user;
|
|
21
|
-
}
|
|
22
|
-
```
|
|
23
|
-
|
|
24
|
-
**With TTL:**
|
|
25
|
-
```javascript
|
|
26
|
-
function withTTL(cache, ttl) {
|
|
27
|
-
return {
|
|
28
|
-
get(key) {
|
|
29
|
-
const item = cache.get(key);
|
|
30
|
-
if (item && Date.now() - item.timestamp < ttl) {
|
|
31
|
-
return item.value;
|
|
32
|
-
}
|
|
33
|
-
return null;
|
|
34
|
-
},
|
|
35
|
-
set(key, value) {
|
|
36
|
-
cache.set(key, { value, timestamp: Date.now() });
|
|
37
|
-
}
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
**Distributed (Redis):**
|
|
43
|
-
```javascript
|
|
44
|
-
async function getCached(key, fetcher, ttl = 300) {
|
|
45
|
-
const cached = await redis.get(key);
|
|
46
|
-
if (cached) return JSON.parse(cached);
|
|
47
|
-
|
|
48
|
-
const value = await fetcher();
|
|
49
|
-
await redis.set(key, JSON.stringify(value), 'EX', ttl);
|
|
50
|
-
return value;
|
|
51
|
-
}
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
---
|
|
55
|
-
|
|
56
|
-
## Lazy Loading
|
|
57
|
-
|
|
58
|
-
### When
|
|
59
|
-
- Large data sets
|
|
60
|
-
- Heavy imports
|
|
61
|
-
- Optional features
|
|
62
|
-
|
|
63
|
-
### Patterns
|
|
64
|
-
|
|
65
|
-
**Pagination:**
|
|
66
|
-
```javascript
|
|
67
|
-
async function getUsers(page = 1, limit = 20) {
|
|
68
|
-
return db.users.findMany({
|
|
69
|
-
take: limit,
|
|
70
|
-
skip: (page - 1) * limit,
|
|
71
|
-
orderBy: { createdAt: 'desc' }
|
|
72
|
-
});
|
|
73
|
-
}
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
**Code splitting:**
|
|
77
|
-
```javascript
|
|
78
|
-
// Before: import everything
|
|
79
|
-
import * as heavy from './heavy';
|
|
80
|
-
|
|
81
|
-
// After: dynamic import
|
|
82
|
-
const heavy = await import('./heavy');
|
|
83
|
-
```
|
|
84
|
-
|
|
85
|
-
**Virtual scrolling:**
|
|
86
|
-
```javascript
|
|
87
|
-
// Only render visible items
|
|
88
|
-
<VirtualList items={users} height={400} itemHeight={40} />
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
---
|
|
92
|
-
|
|
93
|
-
## Connection Pooling
|
|
94
|
-
|
|
95
|
-
### When
|
|
96
|
-
- Database connections
|
|
97
|
-
- HTTP clients
|
|
98
|
-
- WebSocket connections
|
|
99
|
-
|
|
100
|
-
### Patterns
|
|
101
|
-
|
|
102
|
-
```javascript
|
|
103
|
-
import { Pool } from 'pg';
|
|
104
|
-
|
|
105
|
-
const pool = new Pool({
|
|
106
|
-
max: 20,
|
|
107
|
-
idleTimeoutMillis: 30000,
|
|
108
|
-
connectionTimeoutMillis: 2000,
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
// Use throughout
|
|
112
|
-
const result = await pool.query('SELECT * FROM users');
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
---
|
|
116
|
-
|
|
117
|
-
## Batch Operations
|
|
118
|
-
|
|
119
|
-
### When
|
|
120
|
-
- Multiple inserts/updates
|
|
121
|
-
- Multiple API calls
|
|
122
|
-
|
|
123
|
-
### Patterns
|
|
124
|
-
|
|
125
|
-
**Bulk insert:**
|
|
126
|
-
```javascript
|
|
127
|
-
// Before: N queries
|
|
128
|
-
for (const item of items) {
|
|
129
|
-
await db.insert(item);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
// After: 1 query
|
|
133
|
-
await db.insertMany(items);
|
|
134
|
-
```
|
|
135
|
-
|
|
136
|
-
**Batch API calls:**
|
|
137
|
-
```javascript
|
|
138
|
-
// Use Promise.all with limit
|
|
139
|
-
async function batch(items, fn, limit = 10) {
|
|
140
|
-
const results = [];
|
|
141
|
-
for (let i = 0; i < items.length; i += limit) {
|
|
142
|
-
const batch = items.slice(i, i + limit);
|
|
143
|
-
results.push(...await Promise.all(batch.map(fn)));
|
|
144
|
-
}
|
|
145
|
-
return results;
|
|
146
|
-
}
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
---
|
|
150
|
-
|
|
151
|
-
## Memoization
|
|
152
|
-
|
|
153
|
-
```javascript
|
|
154
|
-
function memoize(fn) {
|
|
155
|
-
const cache = new Map();
|
|
156
|
-
return (...args) => {
|
|
157
|
-
const key = JSON.stringify(args);
|
|
158
|
-
if (cache.has(key)) return cache.get(key);
|
|
159
|
-
const result = fn(...args);
|
|
160
|
-
cache.set(key, result);
|
|
161
|
-
return result;
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// Usage
|
|
166
|
-
const fib = memoize(n => n <= 1 ? n : fib(n-1) + fib(n-2));
|
|
167
|
-
```
|