0
0
Node.jsframework~20 mins

CPU profiling basics in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Node.js CPU Profiling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
What does CPU profiling in Node.js primarily measure?
CPU profiling helps developers understand what aspect of their Node.js application?
AThe number of network requests made by the application
BThe amount of memory used by variables during execution
CThe size of the application bundle after build
DThe time spent executing JavaScript functions on the CPU
Attempts:
2 left
💡 Hint
Think about what 'CPU' relates to in a computer.
component_behavior
intermediate
2: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 }));
AAn empty file because no profiling was started
BA JSON file containing start time, end time, and the sum result from busyLoop
CA text file listing all functions called during execution with timestamps
DA binary file with CPU usage statistics for each function call
Attempts:
2 left
💡 Hint
Look at what is being written to the file.
📝 Syntax
advanced
2: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.
A
import inspector from 'node:inspector';
const session = new inspector.Session();
session.connect();
session.post('Profiler.start');
// run code
session.post('Profiler.stop', (err, { profile }) =&gt; {
  console.log(profile);
  session.disconnect();
});
B
import inspector from 'node:inspector';
const session = new inspector.Session();
session.post('Profiler.start');
session.connect();
// run code
session.post('Profiler.stop', (err, { profile }) =&gt; {
  console.log(profile);
  session.disconnect();
});
C
import inspector from 'node:inspector';
const session = new inspector.Session();
session.connect();
// run code
session.post('Profiler.start');
session.post('Profiler.stop', (err, { profile }) =&gt; {
  console.log(profile);
  session.disconnect();
});
D
import inspector from 'node:inspector';
const session = new inspector.Session();
session.connect();
session.post('Profiler.stop');
// run code
session.post('Profiler.start', (err, { profile }) =&gt; {
  console.log(profile);
  session.disconnect();
});
Attempts:
2 left
💡 Hint
Remember to connect the session before starting profiling.
🔧 Debug
advanced
2: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();
});
ABecause Profiler.stop callback is missing error handling, causing silent failure
BBecause session.connect() was called too early, it should be after Profiler.start
CBecause no CPU-intensive code ran between start and stop, profile is empty
DBecause the inspector module does not support CPU profiling in Node.js
Attempts:
2 left
💡 Hint
Think about what profiling measures and what code runs between start and stop.
state_output
expert
3: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();
      });
    });
  });
})();
A4
B3
C2
D1
Attempts:
2 left
💡 Hint
Each function call creates a node plus the root node.