DEV Community

Cover image for use `console.time()` and `console.timeEnd()` to measure the time taken to execute a piece of code, in JS
Abhay Prajapati
Abhay Prajapati

Posted on

use `console.time()` and `console.timeEnd()` to measure the time taken to execute a piece of code, in JS

console.time('time')
console.log('Hello World')
console.timeEnd('time')
// Hello World
// time: 7.991ms
Enter fullscreen mode Exit fullscreen mode

Here in JavaScript we are having cool method called as console.time and console.timeEnd which is used to measure the time taken to execute a piece of code.

  1. start with writing the code in console.time('time'), here are "time" is the name of the variable which we will use to store the time taken to execute the code
  2. Start writing your complex code in console.log('Hello World')
  3. Now I'll end the code with console.timeEnd('time') and we will get the time taken to execute the code

Note: console.time and console.timeEnd() must have same "string" name, which is time here.

It is really handy when you are learning Algorithums and you want to measure the time taken to execute the code.

Let's try it out.

let nums = [];
// it's empty array.
for (let i = 0; i < 1000000; i++) {
    // making a loop over 1 million times; and pushing it.
    nums.push(Math.floor(Math.random() * 1000000));
}
// sorting the arrray
nums.sort(function (a, b) { return a - b });
Enter fullscreen mode Exit fullscreen mode

Solving by Linear Search Algo:

let LinearSearch = function (nums, target) {
    console.time("linear")
    for (let i = 0; i < nums.length; i++) {
        if (nums[i] === target) {
            console.timeEnd("linear")
            return i;
        }
    }
    console.timeEnd("linear")
    return -1;
}
// find million and 1
LinearSearch(nums, 1000001);
console.log(LinearSearch(nums, 1000001));
// linear: 10.044ms
// -1
Enter fullscreen mode Exit fullscreen mode

Example with Binary Search Algo:

// Make a function of binary search
let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let target = 5;


let BinarySearch = function (nums, target) {
    console.time("binary")
    let start = 0;
    let end = nums.length - 1;
    while (start <= end) {
        let mid = Math.floor((start + end) / 2);
        if (nums[mid] === target) {
            console.timeEnd("binary")
            return mid;
        }
        else if (nums[mid] < target) {
            start = mid + 1;
        }
        else {
            end = mid - 1;
        }
    }
    console.timeEnd("binary")
    return -1;
}
console.log(BinarySearch(nums, 1000001))
// binary: 0.129ms
// -1
Enter fullscreen mode Exit fullscreen mode

See it's become so handy far a beginner to understand the concept of time taken by a function.

🫂 Thanks for reading.

If you have any questions, please feel free to contact me; comment on the post below or reach on me on

Twitter : @Abhayprajapati_

Github : @theabhayprajapati

Top comments (0)