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

Discussion (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
LUKESHIRU

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

Collapse
ats1999 profile image
Comment marked as low quality/non-constructive by the community. View Code of Conduct
Rahul kumar

Please have look at my app that i built with next+react+node.

check-here