Home >Web Front-end >JS Tutorial >How Can I Measure a JavaScript Function's Execution Time in Milliseconds?
Measuring Function Execution Time in Milliseconds
To determine the execution time of a function in milliseconds, several effective methods are available.
Performance.now()
The performance.now() function provides a timestamp representing the number of milliseconds elapsed since a specific point in time. To measure the execution time of a function using performance.now():
Note: In Node.js, you need to import the perf_hooks module:
const { performance } = require('perf_hooks');
Console.time()
A more straightforward approach is console.time(). It logs the start time of a measurement and automatically ends it later on. Inside the function you want to measure, use:
The time taken will be displayed in the console. Remember to have the same string passed to both time() and timeEnd() for the timer to work correctly.
Additional Notes:
The above is the detailed content of How Can I Measure a JavaScript Function's Execution Time in Milliseconds?. For more information, please follow other related articles on the PHP Chinese website!