We can take this idea of using a simple trace statement in our code a little further. Most languages support more advanced tracing statements, and some even take that idea to entire separate frameworks, libraries and APIs. Where the intention is, you add these tracing statements into your code not just as a temporary addition to fix an issue, but as a permanent way to habitually monitor specific behavior in your application on an ongoing basis. Now this is an area that gets very language and implementation specific, but I'm going to go through a couple of examples to show what's out there.
I'm actually looking at a Chrome screenshot here. This is simple and it's straight forward and it's easy to do. But if you start to introduce a lot of logging messages, this is tough to tell the important ones from the not so important ones. So you can add more specific console messages which exactly match console.log but you can use debug or info or warn or error. And depending on the browser developer tool which you're using they'll either show up with a color-coded line or with an icon beside them. Having said that, this is the kind of thing we'd really use more when debugging but here, I'm more interested in profiling time and figuring out some performance issues.
We will surround it by console.profile and console.profileEnd. Now this is going to tell the Browser to profile everything it's doing between these two statements. Profiling is a bit more intensive and it can give you a lot of data, so you don't just want to open up a profile and close it around every single thing you'd do, you'd hopefully have been able to narrow it down to a particular area to look at. So I run this, and now what shows up in the debugger is a more substantial profile, breaking down not just the overall time. This one function called Turk, but all the way down internally.
I can see the complex function was called but after that second function was called, and third really complicated function was called, and it's also averaging how much time it's actually taking. So it's actually telling me here that it's spending about 34% of the time in the garbage collector cleaning up unused objects, the rest of the time seems to be spent in the third really complicated Function, which sounds like a place I might want to spend my time. And I'm looking here at Chrome's developer tools. Safari gives me a slightly different view, but it's the same kind of idea. We see the nesting here, we do the page unload.
So if you're using a tool like a minifier to streamline your script files, those tools can usually do that for you. Next, let's look at something a little more advanced.
Learn to choose the right data types, understand the pitfalls of using high-level languages, and decide where to spend your time. Plus, see how the underlying memory management model may have more of an impact than you realize, and what performance issues you can expect working with databases and web services.
- Identifying problems in the code
- Embracing constraints
- Using code analysis tools to measure performance
- Managing memory
- Managing disk-based and network resources