Skip to content

lib: avoid quadratic shift() in startup snapshot callback#62914

Open
watilde wants to merge 1 commit intonodejs:mainfrom
watilde:startup-snapshot
Open

lib: avoid quadratic shift() in startup snapshot callback#62914
watilde wants to merge 1 commit intonodejs:mainfrom
watilde:startup-snapshot

Conversation

@watilde
Copy link
Copy Markdown
Member

@watilde watilde commented Apr 23, 2026

Micro benchmark

$ cat bench-startup-snapshot-callbacks.js
'use strict';

function runWithShift(callbacks) {
  while (callbacks.length > 0) {
    const { 0: cb, 1: data } = callbacks.shift();
    cb(data);
  }
}

function runWithIndex(callbacks) {
  for (let i = 0; i < callbacks.length; i++) {
    const { 0: cb, 1: data } = callbacks[i];
    cb(data);
  }
  callbacks.length = 0;
}

function makeCallbacks(count) {
  return Array.from({ length: count }, (_, j) => [() => {}, j]);
}

function measure(fn, count, iterations) {
  // warmup
  for (let i = 0; i < 50; i++) fn(makeCallbacks(count));

  const start = performance.now();
  for (let i = 0; i < iterations; i++) fn(makeCallbacks(count));
  return performance.now() - start;
}

const COUNTS = [10, 100, 1_000, 10_000, 100_000];

console.log('--- shift() vs for+length=0 ---\n');

for (const count of COUNTS) {
  const iters = Math.max(10, Math.floor(1_000_000 / count));
  const shiftMs = measure(runWithShift, count, iters);
  const indexMs = measure(runWithIndex, count, iters);
  const ratio = shiftMs / indexMs;

  const countStr = `count=${String(count).padStart(6)}`;
  const shiftStr = `${shiftMs.toFixed(2).padStart(10)}ms`;
  const indexStr = `${indexMs.toFixed(2).padStart(8)}ms`;
  const ratioStr = `${ratio.toFixed(ratio >= 10 ? 0 : 1).padStart(5)}x faster`;

  console.log(`${countStr}  ${shiftStr}  vs  ${indexStr}   => ${ratioStr}`);
}

Result

$ ./node bench-startup-snapshot-callbacks.js
--- shift() vs for+length=0 ---

count=    10       58.22ms  vs     49.66ms   =>   1.2x faster
count=   100       42.62ms  vs     32.54ms   =>   1.3x faster
count=  1000       53.96ms  vs     30.92ms   =>   1.7x faster
count= 10000       60.50ms  vs     28.47ms   =>   2.1x faster
count=100000    46747.20ms  vs     50.64ms   =>   923x faster

@nodejs-github-bot
Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. v8 module Issues and PRs related to the "v8" subsystem. labels Apr 23, 2026
@watilde watilde requested a review from joyeecheung April 23, 2026 18:21
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 23, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.57%. Comparing base (d44a71a) to head (100d584).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62914      +/-   ##
==========================================
- Coverage   89.61%   89.57%   -0.04%     
==========================================
  Files         706      704       -2     
  Lines      219203   218122    -1081     
  Branches    41995    41849     -146     
==========================================
- Hits       196445   195393    -1052     
- Misses      14663    14706      +43     
+ Partials     8095     8023      -72     
Files with missing lines Coverage Δ
lib/internal/v8/startup_snapshot.js 95.48% <100.00%> (+0.10%) ⬆️

... and 125 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. v8 module Issues and PRs related to the "v8" subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants