Avatar ·

How to measure function execution time in JavaScript?

Hi everyone! I'm a beginner JS developer and I've run into this problem: I need to understand how long a specific function takes to execute in order to optimize the code. For example, I have a function that sorts an array or processes a lot of data: ```javascript function processData(items) { // some complex code here let result = []; for (let i = 0; i < items.length; i++) { // many operations result.push(items[i] * 2); } return result; } ``` I tried using `console.time()` and `console.timeEnd()`, but I'm not sure if this is the right way. I've also heard about `performance.now()`, but I don't understand the difference. Please advise how to properly measure function execution time in JavaScript? Preferably with code examples and an explanation of which method is best to use.