Running Time Statistics Method in JavaScript

In development, we often need to count and analyze the execution time of the code. JavaScript provides a variety of ways to measure the running time of the code, this article will introduce the use of Date object, console.time() and performance.now() to achieve this goal.

Running time statistics using Date object

The Date object is a built-in object in JavaScript for manipulating dates and times. By using the Date object, we can obtain the current time at the beginning and end of the code, and calculate the difference between the two to obtain the execution time of the code.

Here is a sample code:

	function myFunction() {
	  // 记录开始时间
	  const startTime = new Date();
	
	  // 在这里编写你的业务逻辑
	
	  // 记录结束时间
	  const endTime = new Date();
	
	  // 计算执行时间(以毫秒为单位)
	  const executionTime = endTime - startTime;
	  
	  // 输出执行时间
	  console.log(`代码执行时间:${executionTime} 毫秒`);
	}

In the above example, we created a function myFunction to simulate business logic. At the beginning and end of the function, we use the Date object to record the current time, and calculate the difference between the two to obtain the execution time of the code.

Then, we output the execution time to the console through the console.log() method. You can display the execution time to the user in a suitable way or use it for other operations according to your needs.

Although the Date object can be used to calculate the execution time of the code, due to its low precision, it may not be ideal for scenarios that need to measure more accurate running time. Next, we will introduce higher precision statistical methods.

Run time statistics using console.time()

In order to obtain more accurate running time statistics, JavaScript provides console.time() and console.timeEnd() methods. These two methods are used to start and end timing with millisecond precision.

Here is an example code using console.time() and console.timeEnd():

function myFunction() {
  // 开始计时
  console.time('计时器');

  // 在这里编写你的业务逻辑

  // 结束计时
  console.timeEnd('计时器');
}

In the example above, we start the timer with console.time('timer') and end it with console.timeEnd('timer'). 'Timer' is a name used to identify the timer, you can customize it according to your needs.

During execution, all code between console.time() and console.timeEnd() is timed. When console.timeEnd() executes, the console will output the time the code was executed. Note that timing can only be performed once under the same identifier, that is, console.time() and console.timeEnd() must be used in pairs.

Although console.time() provides higher precision timing capabilities, it cannot superimpose timing. If you need to time multiple time slices in your code and get the execution time of each time slice, you can use the performance.now() method to do so.

Running time statistics using performance.now()

The performance.now() method returns a high-precision timestamp of the current time in milliseconds. performance.now() provides higher precision than previous methods and allows us to measure execution time for multiple time slices.

Here is a sample code:

function myFunction() {
  // 开始计时
  const startTime = performance.now();

  // 在这里编写你的业务逻辑

  // 结束计时
  const endTime = performance.now();

  // 计算执行时间(以毫秒为单位)
  const executionTime = endTime - startTime;

  // 输出执行时间
  console.log(`代码执行时间:${executionTime.toFixed(2)} 毫秒`);
}

In the above example, we use the performance.now() method to get the current high-precision timestamp to record when the code started and ended. Then, by calculating the difference between the two, we get the execution time of the code.

Finally, use console.log() to output the execution time to the console. Use executionTime.toFixed(2) to round two decimals for readability, you can change the number of decimals or format the output as desired.

Summarize:

The execution time of the code can be obtained using a Date object, but with less precision.
The console.time() and console.timeEnd() methods provide millisecond precision and are suitable for simple timing needs.
performance.now() provides higher precision timestamps and allows us to measure execution time for multiple time slices.

Guess you like

Origin blog.csdn.net/weixin_42657666/article/details/131481730