The following example uses the [Async Hooks][] and Performance APIs to measure the actual duration of a Timeout operation (including the amount of time it took to execute the callback).

    1. 'use strict';
    2. const async_hooks = require('async_hooks');
    3. const {
    4. performance,
    5. PerformanceObserver
    6. } = require('perf_hooks');
    7. const set = new Set();
    8. const hook = async_hooks.createHook({
    9. init(id, type) {
    10. if (type === 'Timeout') {
    11. performance.mark(`Timeout-${id}-Init`);
    12. set.add(id);
    13. }
    14. },
    15. destroy(id) {
    16. if (set.has(id)) {
    17. set.delete(id);
    18. performance.mark(`Timeout-${id}-Destroy`);
    19. performance.measure(`Timeout-${id}`,
    20. `Timeout-${id}-Init`,
    21. `Timeout-${id}-Destroy`);
    22. }
    23. }
    24. });
    25. hook.enable();
    26. const obs = new PerformanceObserver((list, observer) => {
    27. console.log(list.getEntries()[0]);
    28. performance.clearMarks();
    29. observer.disconnect();
    30. });
    31. obs.observe({ entryTypes: ['measure'], buffered: true });
    32. setTimeout(() => {}, 1000);