06.07.2017 Views

Mastering JavaScript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Server-Side <strong>JavaScript</strong><br />

There is a cool trick that I love to use. While installing modules from the command<br />

line, we can add the --save flag to add that module's dependency to the package.<br />

json file automatically:<br />

npm install async --save<br />

npm WARN package.json chapter9@1.0.0 No repository field.<br />

npm WARN package.json chapter9@1.0.0 No README data<br />

async@1.5.0 node_modules/async<br />

In the preceding command, we installed the async module with the normal npm<br />

command with a --save flag. There is a corresponding entry automatically created<br />

in package.json:<br />

"dependencies": {<br />

"async": "^1.5.0",<br />

"request": "^2.65.0"<br />

},<br />

<strong>JavaScript</strong> performance<br />

Like any other language, writing correct <strong>JavaScript</strong> code at scale is an involved task.<br />

As the language matures, several of the inherent problems are being taken care of.<br />

There are several exceptional libraries that aid in writing good quality code. For<br />

most serious systems, good code = correct code + high performance code. The demands of<br />

new-generation software systems are high on performance. In this section, we will<br />

discuss a few tools that you can use to analyze your <strong>JavaScript</strong> code and understand<br />

its performance metrics.<br />

We will discuss the following two ideas in this section:<br />

• Profiling: Timing various functions and operations during script-profiling<br />

helps in identifying areas where you can optimize your code<br />

• Network performance: Examining the loading of network resources such as<br />

images, stylesheets, and scripts<br />

<strong>JavaScript</strong> profiling<br />

<strong>JavaScript</strong> profiling is critical to understand performance aspects of various parts of<br />

your code. You can observe timings of the functions and operations to understand<br />

which operation is taking more time. With this information, you can optimize the<br />

performance of time-consuming functions and tune the overall performance of your<br />

code. We will be focusing on the profiling options provided by Chrome's Developer<br />

Tools. There are comprehensive analysis tools that you can use to understand the<br />

performance metrics of your code.<br />

[ 218 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!