Bird
Raised Fist0
Node.jsframework~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of CPU profiling in Node.js?
easy
A. To debug syntax errors in the code
B. To check the memory usage of the application
C. To monitor network requests
D. To find which parts of the code use the most CPU time

Solution

  1. Step 1: Understand CPU profiling goal

    CPU profiling tracks where the CPU spends time during app execution.
  2. Step 2: Compare options to profiling purpose

    Only To find which parts of the code use the most CPU time matches CPU time usage; others relate to memory, network, or syntax.
  3. Final Answer:

    To find which parts of the code use the most CPU time -> Option D
  4. Quick Check:

    CPU profiling = find CPU time hotspots [OK]
Hint: CPU profiling shows where CPU time is spent [OK]
Common Mistakes:
  • Confusing CPU profiling with memory profiling
  • Thinking it tracks network or syntax errors
  • Assuming it shows all app performance issues
2. Which command correctly starts CPU profiling in Node.js?
easy
A. node --profile app.js
B. node --cpu-profile app.js
C. node --prof app.js
D. node --profile-cpu app.js

Solution

  1. Step 1: Recall Node.js CPU profiling command

    The correct flag to start CPU profiling is --prof.
  2. Step 2: Check each option's correctness

    Only node --prof app.js uses --prof, others are invalid flags.
  3. Final Answer:

    node --prof app.js -> Option C
  4. Quick Check:

    Use --prof to start CPU profiling [OK]
Hint: Use --prof flag to enable CPU profiling [OK]
Common Mistakes:
  • Using incorrect flags like --profile or --cpu-profile
  • Confusing profiling with debugging flags
  • Omitting the --prof flag entirely
3. Given this command sequence:
node --prof app.js
Then running:
node --prof-process isolate-0x12345-v8.log
What is the output of --prof-process?
medium
A. The original JavaScript source code
B. A readable report showing CPU time spent in functions
C. A list of all files loaded by Node.js
D. An error message about missing files

Solution

  1. Step 1: Understand purpose of --prof-process

    This command processes the raw CPU profile log into a readable report.
  2. Step 2: Match output to options

    The output is a report showing CPU time spent per function, not source code or file lists.
  3. Final Answer:

    A readable report showing CPU time spent in functions -> Option B
  4. Quick Check:

    --prof-process = readable CPU time report [OK]
Hint: Use --prof-process to get readable CPU report [OK]
Common Mistakes:
  • Expecting source code output from --prof-process
  • Thinking it lists loaded files
  • Confusing it with error output
4. You ran node --prof app.js but no log file was created. What is a likely cause?
medium
A. The app.js script exited too quickly before profiling started
B. You forgot to run node --prof-process first
C. You used --prof with an unsupported Node.js version
D. The log file is created only if you add --cpu-prof

Solution

  1. Step 1: Understand profiling log creation

    The log file is created when the app runs with --prof and exits normally.
  2. Step 2: Analyze why no log appears

    If the app exits too fast, profiling may not start or finish, so no log is saved.
  3. Final Answer:

    The app.js script exited too quickly before profiling started -> Option A
  4. Quick Check:

    App must run long enough to create profile log [OK]
Hint: App must run fully to generate profile log [OK]
Common Mistakes:
  • Thinking --prof-process creates the log file
  • Assuming --cpu-prof is required for logs
  • Blaming Node.js version without checking
5. You want to find which function in your Node.js app uses the most CPU time. You run node --prof app.js and get a log file. What is the correct next step to analyze this data?
hard
A. Run node --prof-process on the log file to get a readable CPU profile report
B. Open the log file in a text editor and search for function names manually
C. Run node --inspect to debug the app instead
D. Restart the app without profiling to compare performance

Solution

  1. Step 1: Understand how to analyze CPU profile logs

    The raw log file is not human-friendly; it needs processing.
  2. Step 2: Use the correct tool for analysis

    Running node --prof-process on the log file converts it into a readable report showing CPU usage per function.
  3. Final Answer:

    Run node --prof-process on the log file to get a readable CPU profile report -> Option A
  4. Quick Check:

    Use --prof-process to analyze CPU profile logs [OK]
Hint: Process logs with --prof-process for readable CPU report [OK]
Common Mistakes:
  • Trying to read raw logs manually
  • Confusing profiling with debugging
  • Restarting app without analyzing logs