Met Console Time
# JavaScript Console console.time() Method
The `console.time()` method is a built-in utility used to start a timer in the browser's console. It is primarily used by developers to measure the execution time of specific code blocks, helping to identify performance bottlenecks and optimize JavaScript applications.
To stop the timer and print the elapsed time (in milliseconds) to the console, you must call the companion method, [console.timeEnd()](met-console-timeend.html).
---
## Syntax
```javascript
console.time(label);
```
### Parameter Values
| Parameter | Type | Description |
| :--- | :--- | :--- |
| `label` | *String* | **Optional.** A label to associate with the timer. If omitted, the default label `"default"` is used. |
---
## Browser Support
The numbers in the table specify the first browser version that fully supports the `console.time()` method.
| Method | Chrome | Edge / IE | Firefox | Safari | Opera |
| :--- | :--- | :--- | :--- | :--- | :--- |
| **console.time()** | Yes | 11 | 10 | 4 | Yes |
> **Note:** To view the output of this method, make sure the browser's developer tools console is open (usually by pressing **F12** or **Ctrl+Shift+I** / **Cmd+Opt+I**).
---
## Code Examples
### Example 1: Basic Timer (Default Label)
If you do not provide a label, the browser starts a timer with the default label `"default"`.
```javascript
// Start the timer
console.time();
// Run a loop to simulate a workload
for (let i = 0; i < 100000; i++) {
// Code execution
}
// Stop the timer and print the result
console.timeEnd();
```
**Console Output:**
```text
default: 2.134ms
```
---
### Example 2: Using a Custom Label
Using custom labels is highly recommended when you want to make your performance logs descriptive and easy to read.
```javascript
// Start a timer labeled "MyTimer"
console.time("MyTimer");
for (let i = 0; i < 100000; i++) {
// Code execution
}
// Stop the "MyTimer" timer
console.timeEnd("MyTimer");
```
**Console Output:**
```text
MyTimer: 1.895ms
```
---
### Example 3: Comparing Code Performance
You can run multiple timers simultaneously by assigning unique labels to each. This is useful for comparing the execution speeds of different algorithms or code structures.
```javascript
let i;
// Test the performance of a 'for' loop
console.time("For Loop Test");
for (i = 0; i < 100000; i++) {
// Code execution
}
console.timeEnd("For Loop Test");
// Test the performance of a 'while' loop
i = 0;
console.time("While Loop Test");
while (i < 100000) {
i++;
}
console.timeEnd("While Loop Test");
```
**Console Output:**
```text
For Loop Test: 1.952ms
While Loop Test: 0.841ms
```
---
## Important Considerations
1. **Matching Labels:** The label passed to `console.timeEnd(label)` must match the label passed to `console.time(label)` exactly. If they do not match, the timer will not stop, and a warning will be logged in the console.
2. **Intermediate Measurements:** If you want to log the elapsed time without stopping the timer completely, you can use `console.timeLog(label)` instead of `console.timeEnd(label)`.
3. **Production Environments:** While `console.time()` is excellent for local debugging and performance tuning, it is best practice to remove or disable these logs in production environments to avoid unnecessary overhead and console clutter.
YouTip