Performance
- Node.js course
- Expert
- Lesson 46/47
- 6 min
Add optimization with V8 flags and flame graph.
The performance layer
In the fourth lesson we add the performance layer. V8 flags change Node.js's engine behavior; --turbofan turns the optimizer on. In this project, instead of setting real flags, we write a performanceLayer function that represents them.
The performanceLayer function returns { flag: '--turbofan', status: 'hazır' }. The output gets a new line --turbofan: hazır. The starting code carries the previous three lessons' solutions. This cumulative growth sets the stage for the final integrationReport. Real performance tuning in production happens as node --turbofan app.js; here modeling stands in for the concept.
Adding the flag layer
The performance layer follows the same pattern as the previous layers: it returns an object and prints to the console in the form 'field: value'. This consistency makes the final integration easy.
Example: console.log(performanceLayer().flag + ': ' + performanceLayer().status). This produces the line --turbofan: hazır. The previous four lines (ExpertProject, 1.0, N-API: hazır, OpenTelemetry: hazır) stay; the new line appends after them. Each layer uses the same object shape: an identifier field (api, tracing, flag) and a status field. This consistency lets the integrationReport function merge all layers into one string.
Practice Add the performance layer
Take the previous setupProject, nativeAddon and distributedLayer solutions. Add function performanceLayer(); return { flag: '--turbofan', status: 'hazır' }. Print console.log(performanceLayer().flag + ': ' + performanceLayer().status).
Starter code
function setupProject() {
return { name: 'ExpertProject', version: '1.0' };
}
console.log(setupProject().name);
console.log(setupProject().version);
function nativeAddon() {
return { api: 'N-API', status: 'hazır' };
}
console.log(nativeAddon().api + ': ' + nativeAddon().status);
function distributedLayer() {
return { tracing: 'OpenTelemetry', status: 'hazır' };
}
console.log(distributedLayer().tracing + ': ' + distributedLayer().status);
// Write the performanceLayer function
// Return an object holding the TurboFan flag
// Print flag and status
// write hereSample solution
function setupProject() {
return { name: 'ExpertProject', version: '1.0' };
}
console.log(setupProject().name);
console.log(setupProject().version);
function nativeAddon() {
return { api: 'N-API', status: 'hazır' };
}
console.log(nativeAddon().api + ': ' + nativeAddon().status);
function distributedLayer() {
return { tracing: 'OpenTelemetry', status: 'hazır' };
}
console.log(distributedLayer().tracing + ': ' + distributedLayer().status);
function performanceLayer() {
return { flag: '--turbofan', status: 'hazır' };
}
console.log(performanceLayer().flag + ': ' + performanceLayer().status);Expected output
ExpertProject 1.0 N-API: hazır OpenTelemetry: hazır --turbofan: hazır
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does the V8 flag --turbofan do?
function performanceLayer() { return {flag: '--turbofan', status: 'hazır'}; }- Dosya siler
- Disables the JIT
- Enables the TurboFan optimizer Correct answer
- Frees memory
Explanation: --turbofan turns on the TurboFan optimizer.
2 Match each piece to its role
| Term | Meaning |
|---|---|
--turbofan |
V8 flag |
performanceLayer |
Layer function |
flag |
Flag name |
TurboFan |
Optimizer |
3 Complete the flag field
return { flag: '____', status: 'hazır' };
return { flag: '--turbofan', status: 'hazır' };
Explanation: The flag field carries '--turbofan'.