Javascript: Measuring Time to Make Informed Decisions

As an engineer, especially when working on enterprise-grade applications, our coding is influenced by many factors. It is no longer about if your function worked or your ability to achieve the desired result. It is rather about writing performant code, thinking about time complexity vs space complexity, scaling, and ensuring that your application is as fast as possible.

The console.time() method is the console class of Node.js. It is used to starts a timer that is used to compute the time taken by a piece of code or function. The method console.timeEnd() is used to stop the timer and output the elapsed time in milliseconds to stdout. The timer can be accurate to the sub-millisecond.

console.time() takes the parameter “label” and the label must be the same name when calling console.timeEnd() to stop the timer and get the time output to the console. If the label name is not the same, it will throw Warning: No such label ‘{label name}’ for console.timeEnd().

To understand this better, let me use the stopwatch concept to illustrate. Let’s assume you wanted to start a race, you would start your stopwatch and when the race ends, you would stop your stopwatch. So console.time() is the same as starting your stopwatch and console.timeEnd() as stopping your stopwatch. Easy right? 

Now let us take an example of merging two objects with time complexity in mind. Remember, it is not about what works but what works in the shortest time.

const object_1 = {
  id: 3,
  age: 34,
  gender: 'male',
  name: 'Ed Sheeran'
const object_2 = {
  car: "Ferrari",
  colour: "red",
  year: "2023"
//Start the timer
const merge_1 = Object.assign(object_1, object_2);
//end the timer

//Start the timer
const merge_2 = {...object_1,...object_1}
//end the timer

In the above code, I merged two objects using Object constructor method Object.assign() and Javascript spread operator.

Time output on the console:

object_assign: 0.257ms
rest_operator: 0.032ms

From the console output, we can see that the rest/spread operator is faster than Object.assign()

And hence we can now make an informed decision. Please note that in this article we are NOT talking about why rest operators should be used over Object.assign(), but about HOW to use console.time! 🙂

Ready for another example?

const arr =  [1,3,4,5,3]

//Start timer
console.time("map") => item)
//End timer

//Start timer
//End timer


map: 0.188ms
forEach: 0.035ms

So we saw how console.time() and console.timeEnd() can be used as a quick debugging tool to identify bottlenecks in our application.

You can check the MDN documentation for console.time() and  console.timeEnd()

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: