New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

perftrace

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

perftrace - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

23

index.js

@@ -35,2 +35,23 @@ const { PerformanceObserver, performance } = require('node:perf_hooks');

module.exports = { TraceEvents };
// Monkey patching require().
// Refs: https://stackoverflow.com/a/42883538
const Module = require('module');
const originalRequire = Module.prototype.require;
function trackRequires(shouldTrackRequires) {
if (shouldTrackRequires) {
Module.prototype.require = function () {
performance.mark(`require("${arguments[0]}")`);
const ret = originalRequire.apply(this, arguments);
performance.measure(`require("${arguments[0]}")`, `require("${arguments[0]}")`);
return ret;
};
} else {
Module.prototype.require = originalRequire;
}
}
module.exports = {
TraceEvents,
trackRequires,
};

2

package.json
{
"name": "perftrace",
"version": "1.0.1",
"version": "1.1.0",
"description": "Record PerformanceEntry objects from Node.js and the Web in the Trace Event Format, so that it can be visualized on https://ui.perfetto.dev.",

@@ -5,0 +5,0 @@ "repository": {

# perftrace
Record [`PerformanceEntry`](https://w3c.github.io/performance-timeline/#dom-performanceentry) objects from [Node.js](https://nodejs.org/api/perf_hooks.html) and the [Web](https://w3c.github.io/performance-timeline) in the [Trace Event Format](https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview), so that it can be visualized on [https://ui.perfetto.dev](https://ui.perfetto.dev).
Record [`PerformanceEntry`](https://w3c.github.io/performance-timeline/#dom-performanceentry) objects from [Node.js](https://nodejs.org/api/perf_hooks.html) and the [Web](https://w3c.github.io/performance-timeline) in the [Trace Event Format](https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview), so that it can be visualized on <https://ui.perfetto.dev> like this!
## Example in [`example.js`](example.js)
A new `TraceEvents` object is created in:
https://github.com/RaisinTen/perftrace/blob/18b4235b2b21d61a5aa167bf5e9c26cd5c6d90dc/example.js#L6
Before the process exits, the `TraceEvents` object is destroyed and the trace events are stringified and printed to `events.json` in:
https://github.com/RaisinTen/perftrace/blob/18b4235b2b21d61a5aa167bf5e9c26cd5c6d90dc/example.js#L8-L12
The code that needs to be profiled can be done using the `peformance.measure()` and `peformance.mark()` APIs from [Node.js](https://nodejs.org/api/perf_hooks.html#performancemarkname-options) and the [Web](https://www.w3.org/TR/user-timing):
```js
peformance.mark("before");
// code that needs to be profiled
performance.measure("after", "before");
```
like it's being done in:
https://github.com/RaisinTen/perftrace/blob/18b4235b2b21d61a5aa167bf5e9c26cd5c6d90dc/example.js#L14-L26
After running `node example.js`, the generated `events.json` file can be opened on [https://ui.perfetto.dev](https://ui.perfetto.dev) for visualization:
![](./example-perfetto.png)
![](./docs/examples/tracing-requires/perfetto.png)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc