Best Practices for Fine Tuning Node.js Performance

OpenShift Partner Nodefly Logo With Node.js turning four this past month, it’s safe to say that the potential of this programming language has begun to prove itself in production. Even now there are a lot of devoted meetups, hackathons and online communities focused on making the most of Node.js for web or app products. And with big-league companies like eBay, Wal-Mart and LinkedIn utilizing its asynchronous power, you know Node.js has widened its spotlight to include Enterprise as well.

Whether you are wading into the Node.js pool for the first time or a seasoned Node.js swimmer, we have a few ideas to help you make the most of your Node.js development. OpenShift and NodeFly make it really easy to monitor your Node.js app with the NodeFly agent. Check out the NodeFly on OpenShift Quickstart page.

Use the right code for the right job

You wouldn’t use a plumber to fix your wiring, and you may not want to use Node.js for every project. Instead, use it where it excels. Node.js is an event driven language which runs on Google's V8 Engine. It delivers non-blocking IO, which is ideal for real-time applications. All major databases and many public apis have Node libraries and Node's great HTTP libraries make it a breeze to interface with any other system. Are you building a web site that will get a lot of traffic? Will there be a lot of transactions? How about an online game? Do you want Chat capability? That’s the sort of stuff that Node.js excels at. You can get better capability and scaling with fewer server resources.

It's good to remember that Node.js is single-threaded. So CPU-heavy tasks like image manipulation or Fourier transforms might be better left to other processes.

Of course, as Node.js matures and we see more libraries introduced, the potential uses will doubtlessly blossom!

I Could’ve Had a V8... Debugger

Since Node.js is built atop V8, you can take advantage of the same debugging capabilities that V8 shares. Simply access the V8 debugger via TCP on port 5858. You can use Node.js’ built-in debugger or a browser or plug-in option. V8's Debugger operates on a TCP protocol so you can connect some great tools like node-inspector, Eclipse, or the Cloud 9 IDE. When you get into more complicated problems debugging is an essential tool and a great way to learn what's actually happening in your code.

Get into async

Asynchronous programming can take a bit of getting used to if you're not already familiar. Using an async workflow module can be the difference between spaghetti code and a well oiled machine. The aptly named "async" is one we use here at NodeFly."

It’s All JavaScript

It's worth remembering that at the end of the day this is all just JavaScript. You can't write good Node.js if you can't write JavaScript. Learn the prototype object model, learn about closure, read about what's coming up in ES6. JavaScript has been around for a while so there are a plethora of resources out there.

Follow the Monitoring Loop

Make monitoring the peanut butter to your development’s jelly; both can be fine on their own but are fantastic together. Of course, the NodeFly APM will allow you to monitor your Node.js product in real-time and therefore improve it in real-time. We call this the Monitoring Loop:

  1. Monitor – create a benchmark, because you can’t manage what you don’t monitor!
  2. Profile – locate, examine and understand the issue. Allow yourself the opportunity to see deficiencies.
  3. Fix – Recode based on what you have learned to remove any blockage. Use a debugging client and tweak your code and improve your app.
  4. Release – send your updated code into the wild… and begin the cycle anew by monitoring!

The trick is to build the same dedication for monitoring as you have for building. Make monitoring as a critical part of your development process as your IDE tests or the chair you sit in.

Explore

There are a lot of libraries, resources, and options for you to discover. While you may be eager to create and build your github repository, you will find that you don’t need to recreate the wheel. Also, Node.js has been around long enough to get a lot of support. Wanting to go beyond self-taught knowledge? You can find Node.js courses online, or find a local meet-up. Perhaps you have questions about how to do a specific tweak to your code. There are discussion groups to assist. Looking for options for auto-scaling PaaS for your app, then OpenShift would be a logical direction The bottom line is that there are resources and community for you to explore – you are not in this alone!

Hopefully these suggestions will allow you to fine-tune your Node.js writing skills as well as your product. If you are using Node.js for projects that best take advantage of its skills, debug to see how it works, and monitor to constantly profile and fix, then your product should not only reach its own potential, but help shed light on the potential of Node.js as well.

More from this author