node/doc/api
Dom Harrington e124b0ff7d
doc: broken PerformanceObserver code sample
The code sample at the top of the "Performance measurements API"
section of the docs does not run.

The code in question:

```js
const { PerformanceObserver, performance } = require('node:perf_hooks');

const obs = new PerformanceObserver((items) => {
  console.log(items.getEntries()[0].duration);
  performance.clearMarks();
});
obs.observe({ type: 'measure' });
performance.measure('Start to Now');

performance.mark('A');
doSomeLongRunningProcess(() => {
  performance.measure('A to Now', 'A');

  performance.mark('B');
  performance.measure('A to B', 'A', 'B');
});
```

If you replace `doSomeLongRunningProcess` with an IIFE with a sleep()
at the top of it, you get this:

```js
const { PerformanceObserver, performance } = require('node:perf_hooks');

const obs = new PerformanceObserver((items) => {
  console.log(items.getEntries()[0].duration);
  performance.clearMarks();
});
obs.observe({ type: 'measure' });
performance.measure('Start to Now');

performance.mark('A');

(async function doSomeLongRunningProcess() {
  await new Promise(r => setTimeout(r, 5000));
  performance.measure('A to Now', 'A');

  performance.mark('B');
  performance.measure('A to B', 'A', 'B');
})()
```

When you run this, you get the following output:

```sh
$ node performance-test.js
17.873416

node:internal/per_context/domexception:53
    ErrorCaptureStackTrace(this);
    ^
DOMException [SyntaxError]: The "A" performance mark has not been set
    at new DOMException (node:internal/per_context/domexception:53:5)
    at __node_internal_ (node:internal/util:695:10)
    at getMark (node:internal/perf/usertiming:65:11)
    at calculateStartDuration (node:internal/perf/usertiming:202:13)
    at measure (node:internal/perf/usertiming:220:7)
    at Performance.measure (node:internal/perf/performance:135:12)
    at /private/tmp/performance-test.js:14:15

Node.js v20.11.1
```

I believe it's due to the call to `performance.clearMarks();` in the
PerformanceObserver callback. If you remove that, it works as expected:

```js
const { PerformanceObserver, performance } = require('node:perf_hooks');

const obs = new PerformanceObserver((items) => {
  console.log(items.getEntries()[0].duration);
});
obs.observe({ type: 'measure' });
performance.measure('Start to Now');

performance.mark('A');

(async function doSomeLongRunningProcess() {
  await new Promise(r => setTimeout(r, 5000));
  performance.measure('A to Now', 'A');

  performance.mark('B');
  performance.measure('A to B', 'A', 'B');
})()
```

```sh
$ node performance-test.js
17.761083
5002.468417
```

PR-URL: https://github.com/nodejs/node/pull/54227
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
2024-11-02 17:05:24 +00:00
..
addons.md
assert.md
async_context.md
async_hooks.md doc,tools: enforce use of node: prefix 2024-07-21 15:44:27 +00:00
buffer.md 2024-10-16, Version 23.0.0 (Current) 2024-10-16 11:05:01 -03:00
child_process.md doc: add note about stdio streams in child_process 2024-10-22 05:05:05 +00:00
cli.md lib: make ALS default to AsyncContextFrame 2024-11-02 15:14:28 +00:00
cluster.md doc: move numCPUs require to top of file in cluster CJS example 2024-07-30 06:13:52 +00:00
console.md doc: add a note on console stream behavior 2024-11-02 12:37:09 +00:00
corepack.md
crypto.md doc: remove mention of ECDH-ES in crypto.diffieHellman 2024-11-02 12:36:25 +00:00
debugger.md
deprecations.md util: fix util.getCallSites plurality 2024-11-02 15:24:56 +00:00
dgram.md 2024-10-24, Version 23.1.0 (Current) 2024-10-24 23:26:25 +02:00
diagnostics_channel.md http: add diagnostic channel http.server.response.created 2024-11-02 13:46:20 +00:00
dns.md doc: add esm examples to node:dns 2024-08-06 18:34:19 +00:00
documentation.md
domain.md
embedding.md doc: improve c++ embedder API doc 2024-11-01 03:43:53 +00:00
errors.md 2024-10-16, Version 22.10.0 (Current) 2024-10-17 00:34:53 +02:00
esm.md 2024-10-16, Version 23.0.0 (Current) 2024-10-16 11:05:01 -03:00
events.md doc: fix events.once() example using AbortSignal 2024-10-09 07:32:00 +00:00
fs.md fs: make dirent.path writable 2024-10-28 08:33:39 +00:00
globals.md 2024-10-16, Version 23.0.0 (Current) 2024-10-16 11:05:01 -03:00
http2.md 2024-10-16, Version 22.10.0 (Current) 2024-10-17 00:34:53 +02:00
http.md doc: fix the return type of outgoingMessage.setHeaders() 2024-10-11 13:03:35 +00:00
https.md doc: add esm examples to node:https 2024-08-19 13:52:40 +00:00
index.md module: add --experimental-strip-types 2024-07-24 16:30:06 +00:00
inspector.md 2024-10-03, Version 20.18.0 'Iron' (LTS) 2024-10-03 19:54:20 +02:00
intl.md
module.md doc: move typescript support to active development 2024-10-27 14:35:11 +00:00
modules.md 2024-10-16, Version 23.0.0 (Current) 2024-10-16 11:05:01 -03:00
n-api.md 2024-10-16, Version 23.0.0 (Current) 2024-10-16 11:05:01 -03:00
net.md 2024-10-24, Version 23.1.0 (Current) 2024-10-24 23:26:25 +02:00
os.md doc: fix broken Android building link 2024-09-15 12:29:18 +00:00
packages.md doc: move dual package shipping docs to separate repo 2024-10-22 20:14:50 +00:00
path.md 2024-08-21, Version 20.17.0 'Iron' (LTS) 2024-08-21 18:14:55 +02:00
perf_hooks.md doc: broken PerformanceObserver code sample 2024-11-02 17:05:24 +00:00
permissions.md src: add receiver to fast api callback methods 2024-09-28 09:46:03 +00:00
process.md doc: move typescript support to active development 2024-10-27 14:35:11 +00:00
punycode.md
querystring.md
readline.md
repl.md doc: remove parseREPLKeyword from REPL documentation 2024-09-22 19:08:53 +00:00
report.md
single-executable-applications.md doc: fix typo in method name in the sea doc 2024-08-15 11:11:01 +00:00
sqlite.md sqlite: add readOnly option 2024-10-31 11:41:14 +00:00
stream.md doc: add write flag when open file as the demo code's intention 2024-11-02 16:55:53 +00:00
string_decoder.md doc: add esm examples to node:string_decoder 2024-10-26 20:36:25 +00:00
synopsis.md
test.md 2024-10-24, Version 23.1.0 (Current) 2024-10-24 23:26:25 +02:00
timers.md timers: document ref option for scheduler.wait 2024-09-02 06:57:02 +00:00
tls.md 2024-10-03, Version 20.18.0 'Iron' (LTS) 2024-10-03 19:54:20 +02:00
tracing.md
tty.md doc,tty: add documentation for ReadStream and WriteStream 2024-07-19 10:53:24 +00:00
typescript.md doc: move typescript support to active development 2024-10-27 14:35:11 +00:00
url.md
util.md util: fix util.getCallSites plurality 2024-11-02 15:24:56 +00:00
v8.md doc: fix typos 2024-09-29 13:15:15 +00:00
vm.md 2024-10-03, Version 20.18.0 'Iron' (LTS) 2024-10-03 19:54:20 +02:00
wasi.md doc,tools: enforce use of node: prefix 2024-07-21 15:44:27 +00:00
webcrypto.md doc: fix webcrypto.md AES-GCM backticks 2024-08-30 22:32:03 +00:00
webstreams.md 2024-08-21, Version 20.17.0 'Iron' (LTS) 2024-08-21 18:14:55 +02:00
worker_threads.md 2024-10-16, Version 22.10.0 (Current) 2024-10-17 00:34:53 +02:00
zlib.md zlib: remove zlib.bytesRead 2024-09-28 23:46:21 +00:00