阿西河

所有教程

公众号
🌙
阿西河前端的公众号

我的收藏

    最近访问  (文章)

      教程列表

      抓包专区
      测试专区

      Node.js perf_hooks 测量加载依赖的耗时

      Node.js perf_hooks 测量加载依赖的耗时

      The following example measures the duration of require() operations to load dependencies:

      'use strict';
      const {
        performance,
        PerformanceObserver
      } = require('perf_hooks');
      const mod = require('module');
      
      // Monkey patch the require function
      mod.Module.prototype.require =
        performance.timerify(mod.Module.prototype.require);
      require = performance.timerify(require);
      
      // Activate the observer
      const obs = new PerformanceObserver((list) => {
        const entries = list.getEntries();
        entries.forEach((entry) => {
          console.log(`require('${entry[0]}')`, entry.duration);
        });
        obs.disconnect();
      });
      obs.observe({ entryTypes: ['function'], buffered: true });
      
      require('some-module');
      

      更多内容请参考:Node.js perf_hooks 性能钩子,或者通过 点击对应菜单 进行查看;


      目录
      本文目录
      目录