Challenge - 5 Problems
Node.js CPU Profiling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate1:30remaining
What does CPU profiling in Node.js primarily measure?
CPU profiling helps developers understand what aspect of their Node.js application?
Attempts:
2 left
💡 Hint
Think about what 'CPU' relates to in a computer.
✗ Incorrect
CPU profiling tracks how much time the processor spends running each function in your code. It helps find slow parts.
❓ component_behavior
intermediate2:00remaining
What output will this Node.js CPU profiling code produce?
Consider this code snippet that uses the built-in profiler API. What will be the main content of the output file?
Node.js
import { writeFileSync } from 'node:fs'; import { performance } from 'node:perf_hooks'; const start = performance.now(); function busyLoop() { let sum = 0; for (let i = 0; i < 1e7; i++) { sum += i; } return sum; } const result = busyLoop(); const end = performance.now(); writeFileSync('profile.cpuprofile', JSON.stringify({ start, end, result }));
Attempts:
2 left
💡 Hint
Look at what is being written to the file.
✗ Incorrect
This code writes a JSON object with timing and result data, not a CPU profile trace.
📝 Syntax
advanced2:30remaining
Which option correctly starts and stops CPU profiling using Node.js inspector module?
Choose the code snippet that properly starts CPU profiling, runs a function, then stops profiling and saves the data.
Attempts:
2 left
💡 Hint
Remember to connect the session before starting profiling.
✗ Incorrect
You must connect the inspector session before calling Profiler.start. Then after running code, call Profiler.stop to get the profile.
🔧 Debug
advanced2:00remaining
Why does this CPU profiling code fail to capture any profile data?
Given this code snippet, why is the CPU profile empty after running?
Node.js
import inspector from 'node:inspector'; const session = new inspector.Session(); session.connect(); session.post('Profiler.start'); // no code executed here session.post('Profiler.stop', (err, { profile }) => { console.log(profile); session.disconnect(); });
Attempts:
2 left
💡 Hint
Think about what profiling measures and what code runs between start and stop.
✗ Incorrect
Profiling captures CPU usage during the time between start and stop. If no code runs, profile is empty.
❓ state_output
expert3:00remaining
What is the value of 'profile.nodes.length' after this profiling session?
This code profiles a function that calls two other functions. How many nodes will the profile contain?
Node.js
import inspector from 'node:inspector'; const session = new inspector.Session(); session.connect(); function a() { for(let i=0; i<1000; i++) {} } function b() { for(let i=0; i<500; i++) {} } function main() { a(); b(); } (async () => { await new Promise(resolve => { session.post('Profiler.start', () => { main(); session.post('Profiler.stop', (err, { profile }) => { console.log(profile.nodes.length); session.disconnect(); resolve(); }); }); }); })();
Attempts:
2 left
💡 Hint
Each function call creates a node plus the root node.
✗ Incorrect
The profile includes nodes for the root, main, a, and b functions, totaling 4 nodes.