{"id":13527199,"url":"https://github.com/hyj1991/v8-profiler-next","last_synced_at":"2025-04-12T23:40:00.201Z","repository":{"id":27841220,"uuid":"115338984","full_name":"hyj1991/v8-profiler-next","owner":"hyj1991","description":"node bindings for the v8 profiler","archived":false,"fork":false,"pushed_at":"2024-01-10T13:31:51.000Z","size":113,"stargazers_count":225,"open_issues_count":4,"forks_count":21,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-12T23:39:54.762Z","etag":null,"topics":["allocation-profile","cpu-profile","sampling-heap-profile","snapshot","v8-profiler"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hyj1991.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-25T13:11:49.000Z","updated_at":"2025-03-31T20:03:04.000Z","dependencies_parsed_at":"2024-01-01T11:25:08.132Z","dependency_job_id":"6eaa5de2-088e-4145-a4c4-b6627bca388d","html_url":"https://github.com/hyj1991/v8-profiler-next","commit_stats":{"total_commits":64,"total_committers":5,"mean_commits":12.8,"dds":0.09375,"last_synced_commit":"fd2a2528dc2f956c753c44e592cd5774713e52ac"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyj1991%2Fv8-profiler-next","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyj1991%2Fv8-profiler-next/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyj1991%2Fv8-profiler-next/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyj1991%2Fv8-profiler-next/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hyj1991","download_url":"https://codeload.github.com/hyj1991/v8-profiler-next/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647254,"owners_count":21139081,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["allocation-profile","cpu-profile","sampling-heap-profile","snapshot","v8-profiler"],"created_at":"2024-08-01T06:01:43.119Z","updated_at":"2025-04-12T23:40:00.167Z","avatar_url":"https://github.com/hyj1991.png","language":"C++","funding_links":[],"categories":["Repository","C++"],"sub_categories":["Performance Profiling/Analysis"],"readme":"# v8-profiler-next\n\n[![npm version](https://img.shields.io/npm/v/v8-profiler-next/latest.svg)](https://www.npmjs.com/package/v8-profiler-next)\n[![Package Quality](http://npm.packagequality.com/shield/v8-profiler-next.svg)](http://packagequality.com/#?package=v8-profiler-next)\n[![Linux/osx build status](https://github.com/hyj1991/v8-profiler-next/workflows/Continuous%20integration/badge.svg?branch=master)](https://github.com/hyj1991/v8-profiler-next/actions?query=branch%3Amaster)\n[![windows build status](https://ci.appveyor.com/api/projects/status/vp54r2t137iirntf?svg=true)](https://ci.appveyor.com/project/hyj1991/v8-profiler-next)\n[![downloads info](https://img.shields.io/npm/dm/v8-profiler-next.svg)](https://www.npmjs.com/package/v8-profiler-next)\n[![license](https://img.shields.io/npm/l/v8-profiler-next.svg)](LICENSE)\n\n## Description\n\nv8-profiler-next provides [node](https://github.com/nodejs/node) bindings for the v8 profiler.\n\n## I. Quick Start\n\n* **Compatibility**\n  * **node version:** v4.x ~ v21.x\n  * **platform:** mac, linux, windows\n\nThis module can also be used in `worker_threads`.\n\n### take cpu profile\n\n```js\n'use strict';\nconst fs = require('fs');\nconst v8Profiler = require('v8-profiler-next');\nconst title = 'good-name';\n\n// set generateType 1 to generate new format for cpuprofile\n// to be compatible with cpuprofile parsing in vscode.\nv8Profiler.setGenerateType(1);\n\n// ex. 5 mins cpu profile\nv8Profiler.startProfiling(title, true);\nsetTimeout(() =\u003e {\n  const profile = v8Profiler.stopProfiling(title);\n  profile.export(function (error, result) {\n    // if it doesn't have the extension .cpuprofile then\n    // chrome's profiler tool won't like it.\n    // examine the profile:\n    //   Navigate to chrome://inspect\n    //   Click Open dedicated DevTools for Node\n    //   Select the profiler tab\n    //   Load your file\n    fs.writeFileSync(`${title}.cpuprofile`, result);\n    profile.delete();\n  });\n}, 5 * 60 * 1000);\n```\n\nGet `.cpuprofile` in `worker_threads`:\n\n```js\n'use strict';\n\nconst fs = require('fs');\nconst path = require('path');\nconst v8Profiler = require('v8-profiler-next');\nconst workerThreads = require('worker_threads');\n\nv8Profiler.setGenerateType(1);\n\nif (workerThreads.isMainThread) {\n  const w = new workerThreads.Worker(__filename, {\n    env: process.env,\n  });\n  v8Profiler.startProfiling('main', true);\n  w.once('exit', code =\u003e {\n    // create cpu profile in main thread\n    const profile = v8Profiler.stopProfiling('main');\n    const mainProfile = path.join(__dirname, 'main.cpuprofile');\n    fs.existsSync(mainProfile) \u0026\u0026 fs.unlinkSync(mainProfile);\n    fs.writeFileSync(mainProfile, JSON.stringify(profile));\n  });\n} else {\n  v8Profiler.startProfiling('worker_threads', true);\n  // create cpu profile in worker_threads\n  const start = Date.now();\n  while (Date.now() - start \u003c 2000) { }\n  const profile = v8Profiler.stopProfiling('worker_threads');\n  const workerProfile = path.join(__dirname, 'worker_threads.cpuprofile');\n  fs.existsSync(workerProfile) \u0026\u0026 fs.unlinkSync(workerProfile);\n  fs.writeFileSync(workerProfile, JSON.stringify(profile));\n}\n```\n\n### take heapsnapshot\n\n```js\n'use strict';\nconst v8Profiler = require('v8-profiler-next');\nconst snapshot = v8Profiler.takeSnapshot();\n// 1. not as stream\nsnapshot.export(function (error, result) {\n\tif (error){\n\t\tconsole.error(error);\n\t\treturn;\n\t}\n\tconsole.log(result);\n\tsnapshot.delete();\n});\n// 2. as stream\nconst transform = snapshot.export();\ntransform.pipe(process.stdout);\ntransform.on('finish', snapshot.delete.bind(snapshot));\n```\n\nGet `.heapsnapshot` in `worker_threads`:\n\n```js\n'use strict';\n\nconst fs = require('fs');\nconst path = require('path');\nconst v8Profiler = require('v8-profiler-next');\nconst workerThreads = require('worker_threads');\n\nfunction createSnapshot(filename) {\n  const snapshot = v8Profiler.takeSnapshot();\n  const file = path.join(__dirname, filename);\n  const transform = snapshot.export();\n  transform.pipe(fs.createWriteStream(file));\n  transform.on('finish', snapshot.delete.bind(snapshot));\n}\n\nif (workerThreads.isMainThread) {\n  const w = new workerThreads.Worker(__filename, {\n    env: process.env,\n  });\n\n  // create heapsnapshot in main thread\n  createSnapshot('main.heapsnapshot');\n\n} else {\n  const start = Date.now();\n  const array = [];\n  while (Date.now() - start \u003c 2000) { array.push(new Array(1e3).fill('*')); }\n\n  // create heapsnapshot in worker_threads\n  createSnapshot('worker_threads.heapsnapshot');\n}\n```\n\n### take allocation profile\n\n**Attention:** If node version \u003c v12.x, please use sampling heap profiling alone without cpu profiling or taking snapshot.\n\n```js\n'use strict';\nconst v8Profiler = require('v8-profiler-next');\n// set a leak array\nconst arraytest = [];\nsetInterval(() =\u003e {\n  arraytest.push(new Array(1e2).fill('*').join());\n}, 20);\n// start 1min sampling profile\nv8Profiler.startSamplingHeapProfiling();\nsetTimeout(() =\u003e {\n\t// stop and get allocation profile\n\tconst profile = v8Profiler.stopSamplingHeapProfiling();\n\t// upload shf.heapprofile into chrome dev tools -\u003e Memory -\u003e ALLOCATION PRODILES\n  require('fs').writeFileSync('./shf.heapprofile', JSON.stringify(profile));\n\tconsole.log(profile);\n}, 60 * 1000);\n```\n\nGet `.heapprofile` in `worker_threads`:\n\n```js\n'use strict';\n\nconst fs = require('fs');\nconst path = require('path');\nconst v8Profiler = require('v8-profiler-next');\nconst workerThreads = require('worker_threads');\n\nif (workerThreads.isMainThread) {\n  const w = new workerThreads.Worker(__filename, {\n    env: process.env,\n  });\n  v8Profiler.startSamplingHeapProfiling();\n  w.once('exit', code =\u003e {\n    // create heap profile in main thread\n    const profile = v8Profiler.stopSamplingHeapProfiling();\n    const mainProfile = path.join(__dirname, 'main.heapprofile');\n    fs.existsSync(mainProfile) \u0026\u0026 fs.unlinkSync(mainProfile);\n    fs.writeFileSync(mainProfile, JSON.stringify(profile));\n  });\n} else {\n  v8Profiler.startSamplingHeapProfiling();\n  // create heap profile in worker_threads\n  const start = Date.now();\n  const array = [];\n  while (Date.now() - start \u003c 2000) { array.push(new Array(1e3).fill('*')); }\n  const profile = v8Profiler.stopSamplingHeapProfiling();\n  const workerProfile = path.join(__dirname, 'worker_threads.heapprofile');\n  fs.existsSync(workerProfile) \u0026\u0026 fs.unlinkSync(workerProfile);\n  fs.writeFileSync(workerProfile, JSON.stringify(profile));\n}\n```\n\n## II. License\n\n[MIT License](LICENSE)\n\nCopyright (c) 2018 team of [v8-profiler](https://github.com/node-inspector/v8-profiler), hyj1991\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyj1991%2Fv8-profiler-next","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhyj1991%2Fv8-profiler-next","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyj1991%2Fv8-profiler-next/lists"}