On this page

    M

    perf_hooks.timerify

    History
    perf_hooks.timerify(fn, options?): void
    Attributes
    options:Object
    A histogram object created using perf_hooks.createHistogram() that will record runtime durations in nanoseconds.

    This property is an extension by Node.js. It is not available in Web browsers.

    Wraps a function within a new function that measures the running time of the wrapped function. A PerformanceObserver must be subscribed to the 'function' event type in order for the timing details to be accessed.

    import { timerify, performance, PerformanceObserver } from 'node:perf_hooks';
    
    function someFunction() {
      console.log('hello world');
    }
    
    const wrapped = timerify(someFunction);
    
    const obs = new PerformanceObserver((list) => {
      console.log(list.getEntries()[0].duration);
    
      performance.clearMarks();
      performance.clearMeasures();
      obs.disconnect();
    });
    obs.observe({ entryTypes: ['function'] });
    
    // A performance timeline entry will be created
    wrapped();
    const {
      timerify,
      performance,
      PerformanceObserver,
    } = require('node:perf_hooks');
    
    function someFunction() {
      console.log('hello world');
    }
    
    const wrapped = timerify(someFunction);
    
    const obs = new PerformanceObserver((list) => {
      console.log(list.getEntries()[0].duration);
    
      performance.clearMarks();
      performance.clearMeasures();
      obs.disconnect();
    });
    obs.observe({ entryTypes: ['function'] });
    
    // A performance timeline entry will be created
    wrapped();

    If the wrapped function returns a promise, a finally handler will be attached to the promise and the duration will be reported once the finally handler is invoked.