DEV Community

Cover image for How fast is your code?
Shuvo
Shuvo

Posted on

How fast is your code?

You ever wanted to know how long does a block of your code takes to execute? Well you have come to the right place.
You can simply wrap your block of code with console.time function to measure how long your code took to execute.

console.time("Label A")
let num = -999999
for(let i = 0; i < 99999; i++){
    num++
}
console.log(num)
console.timeEnd("Label A")
Enter fullscreen mode Exit fullscreen mode

Console.time demo

Make sure to check out my other articles also

Top comments (20)

Collapse
 
nombrekeff profile image
Keff

Yeah, really usefull!

You can also use performance.now in case you need more precision, though you need to do the calculation yourself.

Collapse
 
0shuvo0 profile image
Shuvo

Yes we learn something new everyday 😄

Collapse
 
yatki profile image
Mehmet Yatkı • Edited

The console.log statement on the line 6 would change the result a lot. I'd move that after console.timeEnd, just to make sure console.log doesn't affect my analysis. 🖖

Collapse
 
lgrammel profile image
Lars Grammel

Tools like jsbench.me/ are also great when you need to compare different snippets.

Collapse
 
rahulp772 profile image
Rahul Prajapati

Useful post. Thank you for sharing!

Collapse
 
0shuvo0 profile image
Shuvo

I am really glad you found this helpful

Collapse
 
_pankajc profile image
Pankaj

Thank you for sharing this!

Collapse
 
0shuvo0 profile image
Shuvo

You're most welcome

Collapse
 
blackr1234 profile image
blackr1234

Nice to know there is such feature. Thanks!

Collapse
 
0shuvo0 profile image
Shuvo

if you console.log(console) you will see it has a lot of useful methods

Collapse
 
lgrammel profile image
Lars Grammel

Sweet, thanks for sharing!

Thread Thread
 
0shuvo0 profile image
Shuvo

I am really glad you liked it

Collapse
 
manoj_shukla_10 profile image
Manoj Shukla • Edited

It's amazing. Thanks for sharing. Now I can know how fast I am, I mean my code.

Collapse
 
0shuvo0 profile image
Shuvo

looks really cool

Collapse
 
amk profile image
Amran AL Ketara

Your content is so useful!
I just give you a follow.
Keep g bro!❤

Collapse
 
0shuvo0 profile image
Shuvo

I'm really grateful for that
Thank you

Collapse
 
muhammadzaki693 profile image
muhammadzaki693

Pi

Collapse
 
ziaad25 profile image
ziaad25 • Edited

Thank you for this information ,It's a really new for me
But how can I use it in C++ language?...

Collapse
 
0shuvo0 profile image
Shuvo

I am glad I could help

Some comments may only be visible to logged-in visitors. Sign in to view all comments.