Categories
Featured Development Resources HTML/Javascript

Performance Testing Javscript – Date(), performance.now(), console.time()

It’s important to know how to performance test your JavaScript code as you can identify bottlenecks within your code that could slow down a web app, or cause you to fail a programming test.

Sometimes there are bottlenecks that are not easily identifiable due to errors in libraries use or simply because one JavaScript engine may perform differently than another.

A few days ago we mentioned the Two Sum problem, which is a very important programming problem, and we mentioned two different solutions that can have dramatically different execution times on longer arrays.

Performance Testing JS With Date()

The first way we’ll examine how to test speed Is using the Date() function.

The issue with using Date() is that this function only returns milliseconds instead of microseconds which makes things cumbersome.

var twoSum = function(nums, target) {
    previousNums = {}
    for (const [index, num] of nums.entries()) {
        answer = target - num
        if(previousNums !== undefined) {
            return [previousNums, index]
        }
        previousNums[num] = index
    }
}

var start = +new Date();
for(var i = 0; i < 1000000; i++ ){
    twoSum([2, 5, 11, 13, 4, 15, 3, 8, 1, 7, 10], 12);
};
var end = +new Date()
console.log(end - start)
// 616

There are two methods that we can use and the method of choice for this is performance.now() and console.time()

This example shows using performance.now()

var twoSum = function(nums, target) {
    previousNums = {}
    for (const [index, num] of nums.entries()) {
        answer = target - num
        if(previousNums !== undefined) {
            return [previousNums, index]
        }
        previousNums[num] = index
    }
}

var start = performance.now();
for(var i = 0; i < 1000000; i++ ){
    twoSum([2, 5, 11, 13, 4, 15, 3, 8, 1, 7, 10], 12);
};
var end = performance.now()
console.log(end - start)
// 610.5999999977648

And this example shows using console.time(), the nice thing about console.time() is that it neatly prints things out for you. It does add some more overhead than performance.now().

var twoSum = function(nums, target) {
    previousNums = {}
    for (const [index, num] of nums.entries()) {
        answer = target - num
        if(previousNums !== undefined) {
            return [previousNums, index]
        }
        previousNums[num] = index
    }
}

console.time();
for(var i = 0; i < 1000000; i++ ){
    twoSum([2, 5, 11, 13, 4, 15, 3, 8, 1, 7, 10], 12);
};
console.timeEnd()
// default: 629.76806640625 ms

Now using these two methods you might find that you get drastically different results which is actually ok, just make sure that you’re consistent.

Conclusion

Something that you’ll probably notice even when performing a large number of executions is that often the first tests are slower and then eventually the tests typically become more consistent. 

You’ll also notice that there is always some variance (just look at LeetCode where the same solution could have dramatically different times).

Despite the variance this testing can be useful as it can help you to identify specific issues with your code, and find the best solution.