JavaScript console.time()

JavaScript console.time()

2023-06-23
1 min read

introduction

The time() method in console (console.time()) starts a timer in the console view. It's very helpful to analyze performance of your code.

How to use.

You have to call the console.time() first, then run any code that you want to test. At last, you have to call the console.timeEnd() to get time.

js
console.time();
for (var i = 0; i < 10000; i++) {}
console.timeEnd();

The Result will be default: 0.22607421875 ms in your console, and it will be different every execution. The result displays performance speed as a microsecond.

Add label

If you want to use different label instead of default, you can change it. console.time("my-label"), console.timeEnd("my-label") The important thing is you have pass same string used in console.time() to console.timeEnd().

js
console.time("my-label");
for (var i = 0; i < 10000; i++) {}
console.timeEnd("my-label");

The result will be my-label: 0.22509765625 ms. In the result, Default: is changed to my-label passed to parameter.

Ref

Free open source project made by Youngjin