Performance Testing in JavaScript (in Browser)

Melwin D'Almeida
1 min readAug 16, 2019

Different techniques to test the performance of your JS code

old School Method

This is a straight forward method which uses Date object to find the code run time.

let startTime = new Date();
// Insert the code that needs to be tested
let endTime = new Date();
let runTime = endTime - startTime;
console.log(runtime);

This will print the run time in milli-seconds. This works, but to get even more accurate results, try the following methods.

console.time Method

Wrap you code between console.time() and console.timeEnd() to find the run time of the code in milliseconds. Something as shown below

console.time("for loop");
let arr = [];
for(let i = 0 ; i < 1000000 ; i++){
arr.push(i);
}
console.timeEnd("for loop");

jsPerf — Online Tool

This is an online tool, which allows you to put your JS code snippets and run benchmarks and get the performance. Here’s the link — https://jsperf.com

There are many other libraries and online tools that allow you to benchmark your code, but these three methods should be good enough to cover most of the performance testing activities.

--

--