Dan D Kim

Let's share stories

Timing your functions the Slick 'n Smooth way

2019-08-19 Dan D. Kimdebugging

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.



  ... some logic


Example output:

function1: 6054.14599609375ms

Bam Dab

“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}`)

Bam Dab again