node icon indicating copy to clipboard operation
node copied to clipboard

doc: add esm examples to `node:perf_hooks`

Open mfdebian opened this issue 4 months ago • 2 comments

This PR adds the ESM counterparts of the CJS examples for the Performance measurement APIs documentation.

There is one particular example in which I'd like to get a more in depth feedback, it's the Measuring how long it takes to load dependencies example.

I've done it this way:

import { performance, PerformanceObserver } from 'node:perf_hooks';

// Activate the observer
const obs = new PerformanceObserver((list) => {
  const entries = list.getEntries();
  entries.forEach((entry) => {
    console.log(`import('${entry[0]}')`, entry.duration);
  });
  performance.clearMarks();
  performance.clearMeasures();
  obs.disconnect();
});
obs.observe({ entryTypes: ['function'], buffered: true });

const timedImport = performance.timerify(async (module) => {
  return await import(module);
});

await timedImport('some-module');

(you can try it by replacing 'some-module' with, let's say, 'node:child_process')

Which gets the job done, but would you agree it's a correct counterpart for its CJS example? I feel I might lack more deep knowledge about the differences between require and import.

Thanks in advance and as always best regards!

mfdebian avatar Oct 03 '24 23:10 mfdebian