Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson

Already subscribed? Sign In

Autoplay

    Logging Timing Data to the Console

    mykola bilokonskymykola bilokonsky

    Learn to use console.time with console.timeEnd to get accurate timings of operations in javascript. This tool can help with javascript profiling and performance optimization, and knowing how to use the console to do it means you don't have to pollute your codebase with timers and placeholders.

    chrome-devtoolsChrome DevTools
    javascriptJavaScript
    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    00:00 Something we do a lot when we're profiling our applications, trying to make sure that we've optimized. We know how long everything takes to run. We want to figure out just the durations of the various things we're doing. It can be helpful to time how long something took. Let's say we're going to create an array and we're going to add 10 million objects to that array. Say array.push this new object.

    00:34 I want to know how long this is going to take to do. This is a great place to use one of the additional utility methods on the console. Rather than manually storing the start time and the end time and then doing our own duration computation, we can say console.time, create objects.

    00:58 Then, down here, console.timeend, create objects. As long as we're using the same key, it doesn't matter what the key is. By saying console.time, we've created a timer with this key. Until we call console.timeend with the same key, it's just going to keep counting. Then, when we call console.timeend, it's going to spit out a report for us.

    01:22 You see what that looks like here. It's running. You can see that's the label and it took about three seconds in order to create these 10 million items and push them into our array.

    Discuss

    Discuss