r/learnjavascript • • Oct 27 '20

Best way to measure execution time with Javascript

Post image
205 Upvotes

10 comments sorted by

5

u/drumstix42 Oct 28 '20

Check out: https://developer.mozilla.org/en-US/docs/Web/API/Performance/now

P.S. where is this from? Is this like a daily card homepage or something??

1

u/bear007 Oct 28 '20

It is just a graphic I made yesterday

3

u/drumstix42 Oct 28 '20

This would be a really cool homepage/new tab page for the browser as a Dev!

I love "re-learning" little tidbits or random methods I haven't seen in a while. If you don't do it, I may consider it!

1

u/bear007 Oct 28 '20

I'll publish a new graphic just now on my profile

3

u/TheSpiffySpaceman Oct 28 '20 edited Oct 28 '20

obligatory "execution time for browser code isn't a good indicator of performance, doesn't represent user experience, doesn't account for IoC containers, don't forget about performance.now(), yatta yatta

1

u/bear007 Oct 28 '20

Right but often you don't need such granularity as performance.now provides.

10

u/Morphray Oct 28 '20

Does it bother anyone else that it’s not timerStart and timerEnd? Sorry these console method names fail code review.

5

u/drumstix42 Oct 28 '20

It's mainly because the parameter is a string to "name" the timer. You're starting a timer via the label. "Start" is just implied.

Personally I just use performance.now() and subtract the 2 values.

5

u/subnub99 Oct 27 '20

I remember seeing Ben Awad doing this, before I was creating a date object and subtracting it from the current time when the function finished. Using console.time is so much better

1

u/IAmMuffinKid Oct 28 '20

Would this work on a calendar im making??