JavaScript: Timing the execution of a piece of code
Saulo Dias

Saulo Dias @saulodias

About: Software Developer graduated in Automation and Control Engineering trying way too hard to become a brogrammer.

Location:
Rio de Janeiro, Brazil
Joined:
Apr 29, 2021

JavaScript: Timing the execution of a piece of code

Publish Date: Nov 5 '21
6 0

One of the simplest ways to have a rough idea of how long a piece of code is taking to execute is by using the console methods time, timerLog, and timeEnd.

Example:

console.time("answer time");
// where `answer time` is a label to identify this timer
alert("Click to continue");
console.timeLog("answer time");
alert("Do a bunch of other stuff...");
console.timeEnd("answer time");
Enter fullscreen mode Exit fullscreen mode

Most of the time you just want to time a whole event and don't care about intermediate steps, though. Then, you just need console.time, console.timeEnd.

That's pretty much it.

Comments 0 total

    Add comment