v8-jit
预览截图
安装方式
先用 Prompt 核对来源与依赖。也可下载安装包。
用 AI 助手安装
复制提示词,让助手先核对固定版本和依赖。
查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "v8-jit":https://skillpi.com/zh/skills/deepskill-dcb248f9453198d16db7148b22e0df4bf9908533c518153ba1a83e4ac6c53d7b。安装前核对上游来源和版本:https://github.com/vercel/next.js。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。
来源与配置
github.com
配置方式待核实
请查看原始来源中的适用工具、依赖和权限要求。
安装实测待补充
V8 JIT 优化模式(v8-jit):用于在 Next.js 服务端内部编写高性能 JavaScript,适用于编写或审查 app-render、stream-utils、routing、caching 或任何请求级代码路径中的热点代码。涵盖隐藏类/形状、单态调用点、内联缓存与多态去优化、闭包分配、数组打包,以及使用 --trace-opt / --trace-deopt 进行性能分析。metadata: internal: true;user-invocable: false。
介绍来源: third_party · vercel · 1.0.0 参考来源
SkillPi 解读
- 能完成的任务
- 在 Next.js 服务器内部的写作或优化性能关键代码路径时使用此技能——特别是每个请求的热路径,如渲染,流线,路由和缓存.
- 适合谁
- 来源文档未说明。
- 输入和输出
- 来源文档未说明。
- 软件、账号及付费要求
- 来源文档未说明。
- 限制与区别
- 来源文档未说明。
- 实测状态
- 未测试
文件浏览
Frontmatter
name: v8-jit
description: >
V8 JIT optimization patterns for writing high-performance JavaScript in
Next.js server internals. Use when writing or reviewing hot-path code in
app-render, stream-utils, routing, caching, or any per-request code path.
Covers hidden classes / shapes, monomorphic call sites, inline caches,
megamorphic deopt, closure allocation, array packing, and profiling with
--trace-opt / --trace-deopt.
user-invocable: false
metadata:
internal: trueV8 JIT Optimization
Use this skill when writing or optimizing performance-critical code paths in Next.js server internals — especially per-request hot paths like rendering, streaming, routing, and caching.
Background: V8's Tiered Compilation
V8 compiles JavaScript through multiple tiers:
- Ignition (interpreter) — executes bytecode immediately.
- Sparkplug — fast baseline compiler (no optimization).
- Maglev — mid-tier optimizing compiler.
- Turbofan — full optimizing compiler (speculative, type-feedback-driven).
Code starts in Ignition and is promoted to higher tiers based on execution frequency and collected type feedback. Turbofan produces the fastest machine code but bails out (deopts) when assumptions are violated at runtime.
The key principle: help V8 make correct speculative assumptions by keeping types, shapes, and control flow predictable.
Hidden Classes (Shapes / Maps)
Every JavaScript object has an internal "hidden class" (V8 calls it a Map, the spec calls it a Shape). Objects that share the same property names, added in the same order, share the same hidden class. This enables fast property access via inline caches.
Initialize All Properties in Constructors
// GOOD — consistent shape, single hidden class transition chain
class RequestContext {
url: string
method: string
headers: Record<string, string>
startTime: number
cached: boolean
constructor(url: string, method: string, headers: Record<string, string>) {
this.url = url
this.method = method
this.headers = headers
this.startTime = performance.now()
this.cached = false // always initialize, even defaults
}
}
// BAD — conditional property addition creates multiple hidden classes
class RequestContext {
constructor(url, method, headers, options) {
this.url = url
this.method = method
if (options.timing) {
this.startTime = performance.now() // shape fork!
}
if (options.cache) {
this.cached = false // another shape fork!
}
this.headers = headers
}
}
Rules:
- Assign every property in the constructor, in the same order, for every
instance. Use
null/undefined/falseas default values rather than omitting the property. - Prefer factory functions when constructing hot-path objects. A single factory makes it harder to accidentally fork shapes in different call sites.
- Never
deletea property on a hot object — it forces a transition to dictionary mode (slow properties). - Avoid adding properties after construction (
obj.newProp = x) on objects used in hot paths. - Object literals that flow into the same function should have keys in the same order:
- Use tuples for very small fixed-size records when names are not needed. Tuples avoid key-order pitfalls entirely.
// GOOD — same key order, shares hidden class
const a = { type: 'static', value: 1 }
const b = { type: 'dynamic', value: 2 }
// BAD — different key order, different hidden classes
const a = { type: 'static', value: 1 }
const b = { value: 2, type: 'dynamic' }
Real Codebase Example
Span in src/trace/trace.ts initializes all fields in the constructor in a
fixed order — name, parentId, attrs, status, id, _start, now.
This ensures all Span instances share one hidden class.
Monomorphic vs Polymorphic vs Megamorphic
V8's inline caches (ICs) track the types/shapes seen at each call site or property access:
| IC State | Shapes Seen | Speed |
|---|---|---|
| Monomorphic | 1 | Fastest — single direct check |
| Polymorphic | 2–4 | Fast — linear search through cases |
| Megamorphic | 5+ | Slow — hash-table lookup, no inlining |
Once an IC goes megamorphic it does NOT recover (until the function is re-compiled). Megamorphic ICs also prevent Turbofan from inlining the function.
Keep Hot Call Sites Monomorphic
// GOOD — always called with the same argument shape
function processChunk(chunk: Uint8Array): void {
// chunk is always Uint8Array → monomorphic
}
// BAD — called with different types at the same call site
function processChunk(chunk: Uint8Array | Buffer | string): void {
// IC becomes polymorphic/megamorphic
}
Practical strategies:
- Normalize inputs at the boundary (e.g. convert
Buffer→Uint8Arrayonce) and keep internal functions monomorphic. - Avoid passing both
nullandundefinedfor the same parameter — pick one sentinel value. - When a function must handle multiple types, split into separate specialized functions and dispatch once at the entry point:
// Entry point dispatches once
function handleStream(stream: ReadableStream | Readable) {
if (stream instanceof ReadableStream) {
return handleWebStream(stream) // monomorphic call
}
return handleNodeStream(stream) // monomorphic call
}
This is the pattern used in stream-ops.ts and throughout the stream-utils
code (Node.js vs Web stream split via compile-time switcher).
Closure and Allocation Pressure
Every closure captures its enclosing scope. Creating closures in hot loops or per-request paths generates GC pressure and can prevent escape analysis.
Hoist Closures Out of Hot Paths
// BAD — closure allocated for every request
function handleRequest(req) {
stream.on('data', (chunk) => processChunk(chunk, req.id))
}
// GOOD — shared listener, request context looked up by stream
const requestIdByStream = new WeakMap()
function onData(chunk) {
const id = requestIdByStream.get(this)
if (id !== undefined) processChunk(chunk, id)
}
function processChunk(chunk, id) {
/* ... */
}
function handleRequest(req) {
requestIdByStream.set(stream, req.id)
stream.on('data', onData)
}
// BEST — pre-allocate the callback as a method on a context object
class StreamProcessor {
id: string
constructor(id: string) {
this.id = id
}
handleChunk(chunk: Uint8Array) {
processChunk(chunk, this.id)
}
}
Avoid Allocations in Tight Loops
// BAD — allocates a new object per iteration
for (const item of items) {
doSomething({ key: item.key, value: item.value })
}
// GOOD — reuse a mutable scratch object
const scratch = { key: '', value: '' }
for (const item of items) {
scratch.key = item.key
scratch.value = item.value
doSomething(scratch)
}
Real Codebase Example
node-stream-helpers.ts hoists encoder, BUFFER_TAGS, and tag constants to
module scope to avoid re-creating them on every request. The bufferIndexOf
helper uses Buffer.indexOf (C++ native) instead of a per-call JS loop,
eliminating per-chunk allocation.
Array Optimizations
V8 tracks array "element kinds" — an internal type tag that determines how elements are stored in memory:
| Element Kind | Description | Speed |
|---|---|---|
PACKED_SMI |
Small integers only, no holes | Fastest |
PACKED_DOUBLE |
Numbers only, no holes | Fast |
PACKED_ELEMENTS |
Mixed/objects, no holes | Moderate |
HOLEY_* |
Any of above with holes | Slower (extra bounds check) |
Transitions are one-way — once an array becomes HOLEY or PACKED_ELEMENTS,
it never goes back.
Rules
- Pre-allocate arrays with known size:
new Array(n)creates a holey array. Prefer[]andpush(), or useArray.from({ length: n }, initFn). - Don't create holes:
arr[100] = xon an empty array creates 100 holes. - Don't mix types:
[1, 'two', {}]immediately becomesPACKED_ELEMENTS. - Prefer typed arrays only when you need binary interop/contiguous memory or have profiling evidence that they help. For small/short-lived collections, normal arrays can be faster and allocate less.
// GOOD — packed SMI array
const indices: number[] = []
for (let i = 0; i < n; i++) {
indices.push(i)
}
// BAD — holey from the start
const indices = new Array(n)
for (let i = 0; i < n; i++) {
indices[i] = i
}
Real Codebase Example
accumulateStreamChunks in app-render.tsx uses const staticChunks: Array<Uint8Array> = [] with push() — keeping a packed array of a single type
throughout its lifetime.
Function Optimization and Deopts
Hot-Path Deopt Footguns
argumentsobject: usingargumentsin non-trivial ways (e.g.arguments[i]with variablei, leakingarguments). Use rest params instead.- Type instability at one call site: same operation sees both numbers and strings (or many object shapes) and becomes polymorphic/megamorphic.
eval/with: prevents optimization entirely.- Highly dynamic object iteration: avoid
for...inon hot objects; preferObject.keys()/Object.entries()when possible.
Favor Predictable Control Flow
// GOOD — predictable: always returns same type
function getStatus(code: number): string {
if (code === 200) return 'ok'
if (code === 404) return 'not found'
return 'error'
}
// BAD — returns different types
function getStatus(code: number): string | null | undefined {
if (code === 200) return 'ok'
if (code === 404) return null
// implicitly returns undefined
}
Watch Shape Diversity in switch Dispatch
// WATCH OUT — `node.type` IC can go megamorphic if many shapes hit one site
function render(node) {
switch (node.type) {
case 'div':
return { tag: 'div', children: node.children }
case 'span':
return { tag: 'span', text: node.text }
case 'img':
return { src: node.src, alt: node.alt }
// Many distinct node layouts can make this dispatch site polymorphic
}
}
This pattern is not always bad. Often the main pressure is at the shared
dispatch site (node.type), while properties used only in one branch stay
monomorphic within that branch. Reach for normalization/splitting only when
profiles show this site is hot and polymorphic.
String Operations
- String concatenation in loops is usually fine in modern V8 (ropes make
many concatenations cheap). For binary data, use
Buffer.concat(). - Template literals vs concatenation: equivalent performance in modern V8, but template literals are clearer.
string.indexOf()> regex for simple substring checks.- Reuse RegExp objects: don't create a
new RegExp()inside a hot function — hoist it to module scope.
// GOOD — regex hoisted to module scope
const ROUTE_PATTERN = /^\/api\//
function isApiRoute(path: string): boolean {
return ROUTE_PATTERN.test(path)
}
// BAD — regex recreated on every call
function isApiRoute(path: string): boolean {
return /^\/api\//.test(path) // V8 may or may not cache this
}
Map and Set vs Plain Objects
Mapis faster than plain objects for frequent additions/deletions (avoids hidden class transitions and dictionary mode).Setis faster thanobj[key] = truefor membership checks with dynamic keys.- For static lookups (known keys at module load), plain objects or
Object.freeze({...})are fine — V8 optimizes them as constant. - Never use an object as a map if keys come from user input (prototype pollution risk + megamorphic shapes).
Profiling and Verification
V8 Flags for Diagnosing JIT Issues
# Trace which functions get optimized
node --trace-opt server.js 2>&1 | grep "my-function-name"
# Trace deoptimizations (critical for finding perf regressions)
node --trace-deopt server.js 2>&1 | grep "my-function-name"
# Combined: see the full opt/deopt lifecycle
node --trace-opt --trace-deopt server.js 2>&1 | tee /tmp/v8-trace.log
# Show IC state transitions (verbose)
node --trace-ic server.js 2>&1 | tee /tmp/ic-trace.log
# Print optimized code (advanced)
node --print-opt-code --code-comments server.js
Targeted Profiling in Next.js
# Profile a production build
node --cpu-prof --cpu-prof-dir=/tmp/profiles \
node_modules/.bin/next build
# Profile the server during a benchmark
node --cpu-prof --cpu-prof-dir=/tmp/profiles \
node_modules/.bin/next start &
# ... run benchmark ...
# Analyze in Chrome DevTools: chrome://inspect → Open dedicated DevTools
# Quick trace-deopt check on a specific test
node --trace-deopt $(which jest) --runInBand test/path/to/test.ts \
2>&1 | grep -i "deopt" | head -50
Using % Natives (Development/Testing Only)
With --allow-natives-syntax:
function hotFunction(x) {
return x + 1
}
// Force optimization
%PrepareFunctionForOptimization(hotFunction)
hotFunction(1)
hotFunction(2) % OptimizeFunctionOnNextCall(hotFunction)
hotFunction(3)
// Check optimization status
// 1 = optimized, 2 = not optimized, 3 = always optimized, 6 = maglev
console.log(%GetOptimizationStatus(hotFunction))
Checklist for Hot Path Code Reviews
- [ ] All object properties initialized in constructor/literal, same order
- [ ] No
deleteon hot objects - [ ] No post-construction property additions on hot objects
- [ ] Functions receive consistent types (monomorphic call sites)
- [ ] Type dispatch happens at boundaries, not deep in hot loops
- [ ] No closures allocated inside tight loops
- [ ] Module-scope constants for regex, encoders, tag buffers
- [ ] Arrays are packed (no holes, no mixed types)
- [ ]
Map/Setused for dynamic key collections - [ ] No
argumentsobject — use rest params - [ ]
try/catchat function boundary, not inside tight loops - [ ] String building via array +
join()orBuffer.concat() - [ ] Return types are consistent (no
string | null | undefinedmixes)
Related Skills
$dce-edge— DCE-safe require patterns (compile-time dead code)$runtime-debug— runtime bundle debugging and profiling workflow
当前收录版本
1.0.0
安全与评测信息
质量评测
满分100 · 来自 SkillPi LAB 实验室的评测报告。
亮点
- 提供 V8 JIT 优化的具体策略和真实代码示例
- 适用于 Next.js 服务端性能敏感场景
- 涵盖隐藏类、内联缓存、数组打包等核心优化技术
亮点
- 触发词精准完整,覆盖多种变体
- 边界清晰,不易与其他 Skill 重叠
- 明确说明适用场景和边界条件
亮点
- Phase/步骤清晰,逻辑连贯
- 含端到端示例和完整 frontmatter
- 提供可执行的 Checklist 作为出口条件
亮点
- 工具来源明确,参数正确
- 覆盖主要异常场景和 deopt 问题
- 提供 V8 标志用于诊断和验证
取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。
正文静态检测
100 / 100 · skillpi-20261123-no-r3-v1
正文未命中当前规则。
分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。
权限与使用前检查
根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。
未从主文档识别到上述常见操作,不能据此判断无需权限或没有风险。
安全评级与访问范围
safe · 静态证据核验,不代表运行安全保证。
来源文档未说明。
使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。
用户讨论
还没有已核实的点评。