1 Javascript Hack to Time Your Functions

Check it.


// do stuff




Hello there. My name is Jordan Hansen. I’m from cobalt intelligence today. I’m not going to talk very much about web scraping, but we are going to talk about a little function that I didn’t know about that I would like to share. In fact, I want to do this. Hold on here a lot of times when you’re running any kind of long running function.

And we do that in web scraping quite often. Then you want to know how long it takes, or let’s say one a time, how long something don’t time, how long a function takes or whatever. So a function I didn’t know about for a long time, that’s really helpful is console dot time. It’s just built into the code and you just go console that time and that constant, that time in, so it’s like.

Console at that time, start, you start as the label, whoever it is. I don’t know. Script timer. And then console dot time. End. I’m gonna go script again. Just like that. Let me run it. Okay. That was so blind. Blazingly fast. What? Okay. Oh, I hit clear. Okay. You host like what’s going on here. There we go. So that took four milliseconds to run.

So it starts here. It does this, and it’s great for, let’s see, I do some this for I, and we’ll go like, you know, 20 times. And then we’ll say a wait time. I think it’s still doing its thing. Okay. We’ll wait time out. What this constant random number, because math dot random. What time is 10 Matt that ran and just puts out seven decimal between zero and one.

So we’ll just bump that up. And then we’ve over here, we’ll say random number and then let’s say console dot log. Hey, oh, I will say random numbers though, because see what it is each time, and then we can time the script. So this is super helpful when you’re just trying to time. A lot of times you want to know, especially if you’re trying to figure out why is this thing broken?

Okay. That’s milliseconds. Any of that types of thousand,

there we go. Then we add a second to it. Every time since we’re passing into our timeout function, which takes milliseconds secret there

back, those are still like half a millisecond sort of eight seconds. There we go. Anyway, that’s it. Console dot time. Very handy function. Now the bad thing. Here’s the downfall about console that time you can’t save that variable. Now this doesn’t output anything. There’s no return for. Just print it out.

You can’t say this.

I wish I kind of wanted, at some point I was like, oh, I wish that I knew the output. I can’t remember my use case, but there wasn’t time when I wish I had it. And you can’t do it with it. So that’s the only downfall to it. If you need to save that to put it anywhere, like how long these functions take, you can’t put in a database or whatever.

Let’s see here, undefined. Does it output anything? That’s the one bad thing. So if you’re trying to store how long it takes, you can’t do it this way. The end that’s the end of my little script there. Console dot time. Use it to time your scripts by.