Nodeday 2014: Performance Sins Of Our Abstractions
NodeDay 2014: Performance Sins of our Abstractions
Akbar S. Ahmed | Apr 20, 2014

Node.js performance tips

Fine tuning your deployment for maximum performance is always a top priority. Node.js’ chief mechanic shows you how to put the pedal to the metal.

Speaker

My $.02. Trevor is smart. Really, really smart. This talk was amazing and will be basis for some follow-up posts (once I have a chance to grok everything he discussed.)

Run node with

--gdbjit --gdbjit-full

How to inspect JS in gdb

dgd -d lib/ -d src/ 

Quick v8 performance tips

  • inlined functions cannot contain more than 600 chars (including comments). So wrap your functions with comments before and after, but not in your function.
  • inlined functions must be declared close to the call site. I think he said inlined functions need to be in the same module, but I’m not 100% on that)
  • after n deopts v8 marks the function as not optimized (hence the importance to not allow deoptimizations in long running processes)
  • some events like dns and fs are pseudo async (meaning they tie up a thread in the background)
  • think really hard before you decide to use a synchronous method call (i.e. don’t use sync calls)
  • node uses very little memory, so investigate using Buffers to cache resources (reconstructing resources in memory can be faster than reading from the file system)
  • for i/o intensive scenarios keep track of request requirement ratios by tracing libuv. Track resource ratio per request.

Trever along with every Node contributor in the room repeatedly said to not use sync calls. This should be a wake up call if you’re still depending on anything sync.

Functions all the way down

It’s common practice to nest functions, but don’t do this. Create functions all the way down and unnest callbacks using named functions.

Trevor wrote (psycho-proxy)[github.com/trevnorris/psycho-proxy] to see how close he could get to nginx’s proxy performance. It’s an example of hyper-tuned code, but is not something you should use.

Naming and hoisting functions is more efficient than using anonymous callbacks.

Q&A

Q: Are there any plans to support an engine other than v8?

A: They would like it, but the node api and v8 are tightly coupled, plus v8 is rapidly changing.

Someone from the audience stated that this is not a priority currently.

Additional comments

The amount of GC time I experience is driven by the number of objects and number of references to those objects.

Try to keep things in direct scope.

Create type stable function = call each function with the same params in the same positions with the same types each time. This allows v8 to optimize the function.

Important Point: Coding for Node is different from coding for the web. If you are just creating a normal web app then some of what Trevor suggests is not necessary.

However, if you’re coding for Node then knowing C++ helps a lot. Tools to use include strace, gdb, etc. He listed a bunch of tools but I did not have a chance to write everything down.




Subscribe to our newsletter

Contact Information

ABOUT EXPONENTIAL.IO

We specialize in helping professional developers, like you, expand your skill set. Our courses are focused on enabling you to learn everything necessary to use a new technology in a live, production application.

LOCATION

All courses are made with love in
Palo Alto, CA.

Subscribe to our newsletter