Timing A Function
NodeJS
Medium
3 views
Problem Description
Measure how long a function takes using process.hrtime.bigint().
Constraints
Use hrtime.bigint for better precision.
Official Solution
function work() {
let s = 0;
for (let i = 0; i < 300000; i++) s += i % 7;
return s;
}
const start = process.hrtime.bigint();
work();
const end = process.hrtime.bigint();
const ms = Number(end - start) / 1e6;
console.log('Time: ' + Math.round(ms) + 'ms');
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!