Title: lib: avoid quadratic shift() in startup snapshot callback by watilde · Pull Request #62914 · nodejs/node · GitHub
Open Graph Title: lib: avoid quadratic shift() in startup snapshot callback by watilde · Pull Request #62914 · nodejs/node
X Title: lib: avoid quadratic shift() in startup snapshot callback by watilde · Pull Request #62914 · nodejs/node
Description: 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
Open Graph Description: Micro benchmark $ cat bench-startup-snapshot-callbacks.js 'use strict'; function runWithShift(callbacks) { while (callbacks.length > 0) { const { 0: cb, 1: data } = callbacks.shif...
X Description: Micro benchmark $ cat bench-startup-snapshot-callbacks.js 'use strict'; function runWithShift(callbacks) { while (callbacks.length > 0) { const { 0: cb, 1: data } = ca...
Opengraph URL: https://github.com/nodejs/node/pull/62914
X: @github
Domain: github.com
| route-pattern | /:user_id/:repository/pull/:id/commits/:range(.:format) |
| route-controller | pull_requests |
| route-action | commits |
| fetch-nonce | v2:faab9436-f716-0bd3-3cb9-5e11a04a54cd |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | 8F70:27420E:3612533:48214F9:6A655AE0 |
| html-safe-nonce | 6882df31c468fa3b9dffd6c6577d7fba7d94177739f36079bf4e0242a11383d5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjcwOjI3NDIwRTozNjEyNTMzOjQ4MjE0Rjk6NkE2NTVBRTAiLCJ2aXNpdG9yX2lkIjoiMzc2MzM4NDE4MjAxNjEzNzk1MiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 52ab3e150f9f14a85d46e01b479ce4190142cf72bb93a99032a22fdd987a30ac |
| hovercard-subject-tag | pull_request:3574707540 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/nodejs/node/pull/62914/commits/100d58420fecd235f6bbe803773b9b5605b6d41d |
| twitter:image | https://avatars.githubusercontent.com/u/1716463?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/1716463?s=400&v=4 |
| og:image:alt | Micro benchmark $ cat bench-startup-snapshot-callbacks.js 'use strict'; function runWithShift(callbacks) { while (callbacks.length > 0) { const { 0: cb, 1: data } = callbacks.shif... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 52c76df668885aaff23b50bdca1fa1ea44ac9c1553e888ebc70ff1e4daa4625b |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 309153364422b3c499922d1a2a6404910a58ed8e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width