Skip to content

Measuring Performance with Javascript's

JavaScript, Performance, Timing2 min read

As developers, it's essential to have tools and techniques in our arsenal to measure and optimize the performance of our code. In JavaScript, one powerful tool we can utilize is the method. This method provides a high-resolution timestamp, allowing us to accurately measure the execution time of our JavaScript code. In this article, we will delve into how we can leverage to measure performance and track improvements in our JavaScript applications.

Getting Started with

The method is part of the Performance API, which provides access to various timing-related information and performance-related metrics. It returns the number of milliseconds elapsed since the document started loading. The returned value is a DOMHighResTimeStamp, which is a floating-point value with microsecond precision.

To use, simply call the method within your JavaScript code:

1const startTime =;
3// Code snippet to be measured
5const endTime =;
6const elapsedTime = endTime - startTime;
8console.log(`Execution time: ${elapsedTime} milliseconds`);

In the code snippet above, we capture the start time using before the code we want to measure and then capture the end time after the code execution completes. By subtracting the start time from the end time, we obtain the elapsed time in milliseconds.

Measuring Execution Time

Let's explore a practical example to see how we can measure the performance of a JavaScript function using Suppose we have a function that calculates the nth Fibonacci number recursively:

1function fibonacci(n) {
2 if (n <= 1) return n;
3 return fibonacci(n - 1) + fibonacci(n - 2);

To measure the execution time of this function, we can use as follows:

1const startTime =;
2const result = fibonacci(40);
3const endTime =;
4const elapsedTime = endTime - startTime;
6console.log(`Execution time: ${elapsedTime} milliseconds`);

In this example, we measure the time it takes to calculate the 40th Fibonacci number. By capturing the start and end times and calculating the elapsed time, we can analyze the performance characteristics of our code.

Tracking Performance Improvements

One of the most valuable use cases of is tracking the impact of performance optimizations. Suppose we want to compare the performance of two different approaches to calculate Fibonacci numbers. We can measure the execution time of each approach and observe the improvements. Let's consider an iterative approach to calculate Fibonacci numbers:

1function fibonacciIterative(n) {
2 if (n <= 1) return n;
4 let previous = 0;
5 let current = 1;
7 for (let i = 2; i <= n; i++) {
8 const next = previous + current;
9 previous = current;
10 current = next;
11 }
13 return current;

To compare the two approaches, we can measure the execution time of each function:

1const n = 40;
3const recursiveStartTime =;
4const recursiveResult = fibonacci(n);
5const recursiveEndTime =;
6const recursiveElapsedTime = recursiveEndTime - recursiveStartTime;
8const iterativeStartTime =;
9const iterativeResult = fibonacciIterative(n);
10const iterativeEndTime =;
11const iterativeElapsedTime = iterativeEndTime - iterativeStartTime;
13console.log(`Recursive execution time: ${recursiveElapsedTime} milliseconds`);
14console.log(`Iterative execution time: ${iterativeElapsedTime} milliseconds`);

By measuring the execution time of both approaches, we can determine which one performs better in terms of speed. This information is valuable for making informed decisions about code optimizations.

In Closing

Measuring performance is crucial for optimizing JavaScript applications. With, we can accurately measure the execution time of code snippets and track performance improvements. By capturing start and end times and calculating the elapsed time, we gain insights into the performance characteristics of our code. Whether it's measuring the execution time of a single function or comparing different approaches, empowers us to make informed decisions about performance optimizations.