DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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 (21)

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 Author

Yes we learn something new everyday πŸ˜„

Collapse
 
yatki profile image
Mehmet YatkΔ± • Edited on

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
 
lukeshiru profile image
Luke Shiru

Try perf.link .... way better from my PoV and the UI actually feels like we are in 2021 πŸ˜…

Collapse
 
lgrammel profile image
Lars Grammel

Sweet, thanks for sharing!

Thread Thread
 
0shuvo0 profile image
Shuvo Author

I am really glad you liked it

Collapse
 
0shuvo0 profile image
Shuvo Author

looks really cool

Collapse
 
rahulp772 profile image
Rahul Prajapati

Useful post. Thank you for sharing!

Collapse
 
0shuvo0 profile image
Shuvo Author

I am really glad you found this helpful

Collapse
 
_pankajc profile image
Pankaj

Thank you for sharing this!

Collapse
 
0shuvo0 profile image
Shuvo Author

You're most welcome

Collapse
 
blackr1234 profile image
blackr1234

Nice to know there is such feature. Thanks!

Collapse
 
0shuvo0 profile image
Shuvo Author

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

Collapse
 
manoj_shukla_10 profile image
Manoj Shukla • Edited on

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

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 Author

I'm really grateful for that
Thank you

Collapse
 
muhammadzaki693 profile image
muhammadzaki693

Pi

Collapse
 
ziaad25 profile image
ziaad25 • Edited on

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 Author

I am glad I could help

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

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

β­οΈπŸŽ€ JavaScript Visualized: Promises & Async/Await

async await