Testing Efficiency in JS with Console.time

Greg Thompson
4 min readNov 16, 2020

Junior developers often write code without paying any mind to an application’s efficiency, focusing rather on creating usable code first and foremost. While that can be okay sometimes, as you get into more advanced projects it is certainly important to consider time complexity as you build code. Today we’ll look at a simple bubble sort and how we can measure time it takes to complete parts of code using a nice, built-in JS method — console.time.

Building a Simple Bubble Sort Function

Sorting can be a pretty time-intensive task, so we’ll build out two similar sorting functions that we can use to compare time to complete a sort.

The basic premise of a bubble sort is that we iterate through an array from start to end, comparing two elements at each position. If the second element is greater than the first, we’ll swap the two elements. The number of times we must do this is equal to the number of elements in the array. In the case that the element with the smallest value is at the end of the array, it will move one position closer to the beginning of the array each time we loop through the array. Here is that simple algorithm in action:

const bubbleSort = (a) => {
for (let r = 0; r < a.length - 1; r++) {
for (let i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
[a[i], a[i + 1]] = [a[i + 1], a[i]];
}
}
}
return a;
}

Because this sort function loops a total of <arrayLength> times <arrayLength> times, it has time complexity of O(n²). To make things a little neater in terms of time complexity, let’s consider a nice little property of bubble sort. The first time you iterate through the array, the largest element will be sorted to the end of the array. On the second loop through, the second largest element will be sorted to the end of the array, where it will be compared against the largest element. Obviously those two elements will not change positions. The same can be said for the third largest element and beyond. Because of this, we can consider one less element on each successive iteration through the array as we sort. The implementation for that can be found in the modified bubble sort function below.

const bubbleSortFlag = (a) => {
let round = 1;
for (let r = 0; r < a.length - 1; r++) {
for (let i = 0; i < a.length - round; i++) {
if (a[i] > a[i + 1]) {
[a[i], a[i + 1]] = [a[i + 1], a[i]];
}
}
round++;
}
return a;
};

Using console.time to Measure Time Taken for Code Blocks

Now let’s use console.time to measure the time it takes to run our sort functions.

console.time('run timer');for (let i = 0; i < 1000; i++) {
console.log('testing a timer');
}
console.timeEnd('run timer');

The code we want to run will be nested between a two console.time . The strings passed to each of those lines must match or the console will throw an error. After the function finishes we’ll get a response in the console that looks like this:

run timer: 207.587890625 ms

To test the time it takes to sort, I’ve created a helper function that creates an array of random numbers that I’ll use to sort through. Even though sorting the 1,000 element array that I’ll be creating does not have a great time complexity, the computer still gets through it in lightning speed, so I’ll be testing the time it takes to sort 1000 arrays. Without continuing to bore you with the details of how I’m doing this.

Testing Results

After sorting arrays with my first bubbleSort function, on average it took 7700 ms to sort arrays of 1000 elements 1000 times. When I decreased the size of the arrays being sorted to 100 elements, it took on average 81 ms to sort 1000 times. This is almost spot on with what I’d expect, since decreasing the size of the array by 10 would theoretically decrease the sort time by 100.

When using the second bubbleSort function, it took on average 4400 ms to sort arrays of 1000 elements 1000 times. This was much faster than the first algorithm that didn’t consider a decrementing sort. Sorting arrays of 100 elements 1000 times took 55 ms on average. There are certainly other ways to increase the efficiency of sorting through arrays.

Conclusion

Console.time is an extremely useful tool for quickly testing the time it takes functions to run, and can be helpful in determining if any complex or asynchronous functions you are working with could be improved upon.

--

--