Timing your functions the Slick 'n Smooth way
Time your functions
Sometimes we want to time our functions and see how long they take.
Here’s the traditional way:
const start = new Date().getTime()
{
... some logic
}
const end = new Date().getTime();
console.log(end - start);
Simple. We get the time before we execute our logic. Get the time after. Get the difference between those two. That’s our function execution.
Not bad.
And then there’s console.time
.
Console.time
console.time("function1");
{
... some logic
}
console.timeEnd("function1");
Example output:
function1: 6054.14599609375ms
“But Dan, we use a different logging library on our backend. We don’t use the console!”
Then use Moment
const startTime = moment()
...
const duration = moment().diff(startTime, 'seconds')
customLogger.log(`function duration: ${duration}`)