Sunday, September 24, 2023

JavaScript Execution Time

How to Measure JavaScript Execution Time - DEV Community

If measuring in Node, process.hrtime.bigint() returns accuracy in nanoseconds.
Standard timers return number of milliseconds from 1970-01-01T00:00:00

// DATE TIMER
const start = Date.now();
await functionToBeMeasured();
const end = Date.now();
console.log(`Execution time: ${end - start} ms`);

// CONSOLE TIMER
console.time('Execution Time');
await functionToBeMeasured();
console.timeEnd('Execution Time');

// PERFORMANCE. TIMER
const start = performance.now();
await functionToBeMeasured();
const end = performance.now();
console.log(`Execution time: ${end - start} ms`);

No comments: