Search code examples
javascriptperformanceecmascript-5

How to measure import execution time


How can I measure execution time of import without stepping inside it?

Let say I have code bellow.


main.js

const now = Date.now();
import './module';
console.log(`${Date.now() - now}ms`);
// shows 0ms

module.js

const now = Date.now();
let a = 0;
for(let i = 0; i < 1000 * 1000 * 1000, i++) a++;
console.log(`${Date.now() - now}ms`;
// shows 1000ms

Solution

  • The best way I found to measure imported module execution time, is using require instead. It is good to determine slow modules and revert back to import.

    const now = Date.now();
    const module1 = require('./module1');
    console.log(`module1: ${Date.now() - now}ms`);
    const module2 = require('./module2');
    console.log(`module2: ${Date.now() - now}ms`);