| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent c3f9c7a commit 684d15e
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -7,7 +7,11 @@ | |||
| 7 | 7 | The `node:v8` module exposes APIs that are specific to the version of [V8][] | |
| 8 | 8 | built into the Node.js binary. It can be accessed using: | |
| 9 | 9 | ||
| 10 | - ```js | ||
| 10 | + ```mjs | ||
| 11 | + import v8 from 'node:v8'; | ||
| 12 | + ``` | ||
| 13 | + | ||
| 14 | + ```cjs | ||
| 11 | 15 | const v8 = require('node:v8'); | |
| 12 | 16 | ``` | |
| 13 | 17 | ||
@@ -92,9 +96,18 @@ terminating the process. | |||
| 92 | 96 | Generating a snapshot is a synchronous operation which blocks the event loop | |
| 93 | 97 | for a duration depending on the heap size. | |
| 94 | 98 | ||
| 99 | + ```mjs | ||
| 100 | + // Print heap snapshot to the console | ||
| 101 | + import { getHeapSnapshot } from 'node:v8'; | ||
| 102 | + import process from 'node:process'; | ||
| 103 | + const stream = getHeapSnapshot(); | ||
| 104 | + stream.pipe(process.stdout); | ||
| 105 | + ``` | ||
| 106 | + | ||
| 95 | 107 | ```js | |
| 96 | 108 | // Print heap snapshot to the console | |
| 97 | 109 | const v8 = require('node:v8'); | |
| 110 | + const process = require('node:process'); | ||
| 98 | 111 | const stream = v8.getHeapSnapshot(); | |
| 99 | 112 | stream.pipe(process.stdout); | |
| 100 | 113 | ``` | |
@@ -458,11 +471,70 @@ The V8 options available for a version of Node.js may be determined by running | |||
| 458 | 471 | ||
| 459 | 472 | Usage: | |
| 460 | 473 | ||
| 461 | - ```js | ||
| 462 | - // Print GC events to stdout for one minute. | ||
| 463 | - const v8 = require('node:v8'); | ||
| 464 | - v8.setFlagsFromString('--trace_gc'); | ||
| 465 | - setTimeout(() => { v8.setFlagsFromString('--notrace_gc'); }, 60e3); | ||
| 474 | + ```mjs | ||
| 475 | + import { setFlagsFromString } from 'node:v8'; | ||
| 476 | + import { setInterval } from 'node:timers'; | ||
| 477 | + | ||
| 478 | + // setFlagsFromString to trace garbage collection events | ||
| 479 | + setFlagsFromString('--trace-gc'); | ||
| 480 | + | ||
| 481 | + // Trigger GC events by using some memory | ||
| 482 | + let arrays = []; | ||
| 483 | + const interval = setInterval(() => { | ||
| 484 | + for (let i = 0; i < 500; i++) { | ||
| 485 | + arrays.push(new Array(10000).fill(Math.random())); | ||
| 486 | + } | ||
| 487 | + | ||
| 488 | + if (arrays.length > 5000) { | ||
| 489 | + arrays = arrays.slice(-1000); | ||
| 490 | + } | ||
| 491 | + | ||
| 492 | + console.log(`\n* Created ${arrays.length} arrays\n`); | ||
| 493 | + }, 100); | ||
| 494 | + | ||
| 495 | + // setFlagsFromString to stop tracing GC events after 1.5 seconds | ||
| 496 | + setTimeout(() => { | ||
| 497 | + setFlagsFromString('--notrace-gc'); | ||
| 498 | + console.log('\nStopped tracing!\n'); | ||
| 499 | + }, 1500); | ||
| 500 | + | ||
| 501 | + // Stop triggering GC events altogether after 2.5 seconds | ||
| 502 | + setTimeout(() => { | ||
| 503 | + clearInterval(interval); | ||
| 504 | + }, 2500); | ||
| 505 | + ``` | ||
| 506 | + | ||
| 507 | + ```cjs | ||
| 508 | + const { setFlagsFromString } = require('node:v8'); | ||
| 509 | + const { setInterval } = require('node:timers'); | ||
| 510 | + | ||
| 511 | + // setFlagsFromString to trace garbage collection events | ||
| 512 | + setFlagsFromString('--trace-gc'); | ||
| 513 | + | ||
| 514 | + // Trigger GC events by using some memory | ||
| 515 | + let arrays = []; | ||
| 516 | + const interval = setInterval(() => { | ||
| 517 | + for (let i = 0; i < 500; i++) { | ||
| 518 | + arrays.push(new Array(10000).fill(Math.random())); | ||
| 519 | + } | ||
| 520 | + | ||
| 521 | + if (arrays.length > 5000) { | ||
| 522 | + arrays = arrays.slice(-1000); | ||
| 523 | + } | ||
| 524 | + | ||
| 525 | + console.log(`\n* Created ${arrays.length} arrays\n`); | ||
| 526 | + }, 100); | ||
| 527 | + | ||
| 528 | + // setFlagsFromString to stop tracing GC events after 1.5 seconds | ||
| 529 | + setTimeout(() => { | ||
| 530 | + console.log('\nStopped tracing!\n'); | ||
| 531 | + setFlagsFromString('--notrace-gc'); | ||
| 532 | + }, 1500); | ||
| 533 | + | ||
| 534 | + // Stop triggering GC events altogether after 2.5 seconds | ||
| 535 | + setTimeout(() => { | ||
| 536 | + clearInterval(interval); | ||
| 537 | + }, 2500); | ||
| 466 | 538 | ``` | |
| 467 | 539 | ||
| 468 | 540 | ## `v8.stopCoverage()` | |
@@ -542,13 +614,37 @@ terminating the process. | |||
| 542 | 614 | Generating a snapshot is a synchronous operation which blocks the event loop | |
| 543 | 615 | for a duration depending on the heap size. | |
| 544 | 616 | ||
| 545 | - ```js | ||
| 617 | + ```mjs | ||
| 618 | + import { writeHeapSnapshot } from 'node:v8'; | ||
| 619 | + import { Worker, isMainThread, parentPort } from 'node:worker_threads'; | ||
| 620 | + import { fileURLToPath } from 'node:url'; | ||
| 621 | + | ||
| 622 | + if (isMainThread) { | ||
| 623 | + const __filename = fileURLToPath(import.meta.url); | ||
| 624 | + const worker = new Worker(__filename); | ||
| 625 | + | ||
| 626 | + worker.once('message', (filename) => { | ||
| 627 | + console.log(`worker heapdump: ${filename}`); | ||
| 628 | + // Now get a heapdump for the main thread. | ||
| 629 | + console.log(`main thread heapdump: ${writeHeapSnapshot()}`); | ||
| 630 | + }); | ||
| 631 | + | ||
| 632 | + // Tell the worker to create a heapdump. | ||
| 633 | + worker.postMessage('heapdump'); | ||
| 634 | + } else { | ||
| 635 | + parentPort.once('message', (message) => { | ||
| 636 | + if (message === 'heapdump') { | ||
| 637 | + // Generate a heapdump for the worker | ||
| 638 | + // and return the filename to the parent. | ||
| 639 | + parentPort.postMessage(writeHeapSnapshot()); | ||
| 640 | + } | ||
| 641 | + }); | ||
| 642 | + } | ||
| 643 | + ``` | ||
| 644 | + | ||
| 645 | + ```cjs | ||
| 546 | 646 | const { writeHeapSnapshot } = require('node:v8'); | |
| 547 | - const { | ||
| 548 | - Worker, | ||
| 549 | - isMainThread, | ||
| 550 | - parentPort, | ||
| 551 | - } = require('node:worker_threads'); | ||
| 647 | + const { Worker, isMainThread, parentPort } = require('node:worker_threads'); | ||
| 552 | 648 | ||
| 553 | 649 | if (isMainThread) { | |
| 554 | 650 | const worker = new Worker(__filename); | |
@@ -892,6 +988,71 @@ const stopHookSet = promiseHooks.createHook({ | |||
| 892 | 988 | after, | |
| 893 | 989 | }); | |
| 894 | 990 | ||
| 991 | + // Trigger the hooks by using promises | ||
| 992 | + const promiseLog = (word) => Promise.resolve(word).then(console.log); | ||
| 993 | + promiseLog('Hello'); | ||
| 994 | + promiseLog('World'); | ||
| 995 | + | ||
| 996 | + // To stop a hook, call the function returned at its creation. | ||
| 997 | + stopWatchingInits(); | ||
| 998 | + stopWatchingSettleds(); | ||
| 999 | + stopWatchingBefores(); | ||
| 1000 | + stopWatchingAfters(); | ||
| 1001 | + stopHookSet(); | ||
| 1002 | + ``` | ||
| 1003 | + | ||
| 1004 | + ```cjs | ||
| 1005 | + const { promiseHooks } = require('node:v8'); | ||
| 1006 | + | ||
| 1007 | + // There are four lifecycle events produced by promises: | ||
| 1008 | + | ||
| 1009 | + // The `init` event represents the creation of a promise. This could be a | ||
| 1010 | + // direct creation such as with `new Promise(...)` or a continuation such | ||
| 1011 | + // as `then()` or `catch()`. It also happens whenever an async function is | ||
| 1012 | + // called or does an `await`. If a continuation promise is created, the | ||
| 1013 | + // `parent` will be the promise it is a continuation from. | ||
| 1014 | + function init(promise, parent) { | ||
| 1015 | + console.log('a promise was created', { promise, parent }); | ||
| 1016 | + } | ||
| 1017 | + | ||
| 1018 | + // The `settled` event happens when a promise receives a resolution or | ||
| 1019 | + // rejection value. This may happen synchronously such as when using | ||
| 1020 | + // `Promise.resolve()` on non-promise input. | ||
| 1021 | + function settled(promise) { | ||
| 1022 | + console.log('a promise resolved or rejected', { promise }); | ||
| 1023 | + } | ||
| 1024 | + | ||
| 1025 | + // The `before` event runs immediately before a `then()` or `catch()` handler | ||
| 1026 | + // runs or an `await` resumes execution. | ||
| 1027 | + function before(promise) { | ||
| 1028 | + console.log('a promise is about to call a then handler', { promise }); | ||
| 1029 | + } | ||
| 1030 | + | ||
| 1031 | + // The `after` event runs immediately after a `then()` handler runs or when | ||
| 1032 | + // an `await` begins after resuming from another. | ||
| 1033 | + function after(promise) { | ||
| 1034 | + console.log('a promise is done calling a then handler', { promise }); | ||
| 1035 | + } | ||
| 1036 | + | ||
| 1037 | + // Lifecycle hooks may be started and stopped individually | ||
| 1038 | + const stopWatchingInits = promiseHooks.onInit(init); | ||
| 1039 | + const stopWatchingSettleds = promiseHooks.onSettled(settled); | ||
| 1040 | + const stopWatchingBefores = promiseHooks.onBefore(before); | ||
| 1041 | + const stopWatchingAfters = promiseHooks.onAfter(after); | ||
| 1042 | + | ||
| 1043 | + // Or they may be started and stopped in groups | ||
| 1044 | + const stopHookSet = promiseHooks.createHook({ | ||
| 1045 | + init, | ||
| 1046 | + settled, | ||
| 1047 | + before, | ||
| 1048 | + after, | ||
| 1049 | + }); | ||
| 1050 | + | ||
| 1051 | + // Trigger the hooks by using promises | ||
| 1052 | + const promisePrint = (word) => Promise.resolve(word).then(console.log); | ||
| 1053 | + promisePrint('Hello'); | ||
| 1054 | + promisePrint('World'); | ||
| 1055 | + | ||
| 895 | 1056 | // To stop a hook, call the function returned at its creation. | |
| 896 | 1057 | stopWatchingInits(); | |
| 897 | 1058 | stopWatchingSettleds(); | |
@@ -1383,7 +1544,16 @@ is as follows. | |||
| 1383 | 1544 | ||
| 1384 | 1545 | Here's an example. | |
| 1385 | 1546 | ||
| 1386 | - ```js | ||
| 1547 | + ```mjs | ||
| 1548 | + import { GCProfiler } from 'node:v8'; | ||
| 1549 | + const profiler = new GCProfiler(); | ||
| 1550 | + profiler.start(); | ||
| 1551 | + setTimeout(() => { | ||
| 1552 | + console.log(profiler.stop()); | ||
| 1553 | + }, 1000); | ||
| 1554 | + ``` | ||
| 1555 | + | ||
| 1556 | + ```cjs | ||
| 1387 | 1557 | const { GCProfiler } = require('node:v8'); | |
| 1388 | 1558 | const profiler = new GCProfiler(); | |
| 1389 | 1559 | profiler.start(); | |
@@ -1408,8 +1578,37 @@ otherwise, it returns false. | |||
| 1408 | 1578 | If this method returns false, that does not mean that the string contains some characters not in `Latin-1/ISO-8859-1`. | |
| 1409 | 1579 | Sometimes a `Latin-1` string may also be represented as `UTF16`. | |
| 1410 | 1580 | ||
| 1411 | - ```js | ||
| 1581 | + ```mjs | ||
| 1582 | + import { isStringOneByteRepresentation } from 'node:v8'; | ||
| 1583 | + import { Buffer } from 'node:buffer'; | ||
| 1584 | + | ||
| 1585 | + const Encoding = { | ||
| 1586 | + latin1: 1, | ||
| 1587 | + utf16le: 2, | ||
| 1588 | + }; | ||
| 1589 | + const buffer = Buffer.alloc(100); | ||
| 1590 | + function writeString(input) { | ||
| 1591 | + if (isStringOneByteRepresentation(input)) { | ||
| 1592 | + console.log(`input: '${input}'`); | ||
| 1593 | + buffer.writeUint8(Encoding.latin1); | ||
| 1594 | + buffer.writeUint32LE(input.length, 1); | ||
| 1595 | + buffer.write(input, 5, 'latin1'); | ||
| 1596 | + console.log(`decoded: '${buffer.toString('latin1', 5, 5 + input.length)}'\n`); | ||
| 1597 | + } else { | ||
| 1598 | + console.log(`input: '${input}'`); | ||
| 1599 | + buffer.writeUint8(Encoding.utf16le); | ||
| 1600 | + buffer.writeUint32LE(input.length * 2, 1); | ||
| 1601 | + buffer.write(input, 5, 'utf16le'); | ||
| 1602 | + console.log(`decoded: '${buffer.toString('utf16le', 5, 5 + input.length * 2)}'`); | ||
| 1603 | + } | ||
| 1604 | + } | ||
| 1605 | + writeString('hello'); | ||
| 1606 | + writeString('你好'); | ||
| 1607 | + ``` | ||
| 1608 | + | ||
| 1609 | + ```cjs | ||
| 1412 | 1610 | const { isStringOneByteRepresentation } = require('node:v8'); | |
| 1611 | + const { Buffer } = require('node:buffer'); | ||
| 1413 | 1612 | ||
| 1414 | 1613 | const Encoding = { | |
| 1415 | 1614 | latin1: 1, | |
@@ -1418,13 +1617,17 @@ const Encoding = { | |||
| 1418 | 1617 | const buffer = Buffer.alloc(100); | |
| 1419 | 1618 | function writeString(input) { | |
| 1420 | 1619 | if (isStringOneByteRepresentation(input)) { | |
| 1620 | + console.log(`input: '${input}'`); | ||
| 1421 | 1621 | buffer.writeUint8(Encoding.latin1); | |
| 1422 | 1622 | buffer.writeUint32LE(input.length, 1); | |
| 1423 | 1623 | buffer.write(input, 5, 'latin1'); | |
| 1624 | + console.log(`decoded: '${buffer.toString('latin1', 5, 5 + input.length)}'\n`); | ||
| 1424 | 1625 | } else { | |
| 1626 | + console.log(`input: '${input}'`); | ||
| 1425 | 1627 | buffer.writeUint8(Encoding.utf16le); | |
| 1426 | 1628 | buffer.writeUint32LE(input.length * 2, 1); | |
| 1427 | 1629 | buffer.write(input, 5, 'utf16le'); | |
| 1630 | + console.log(`decoded: '${buffer.toString('utf16le', 5, 5 + input.length * 2)}'`); | ||
| 1428 | 1631 | } | |
| 1429 | 1632 | } | |
| 1430 | 1633 | writeString('hello'); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments