The performance tuning aspects are 100% from Trevor Norris who's talk on Performance Sins of our Abstractions at NodeDay validated the benefits of an unnested approach and served as the inspiration for this talk. If you're really into high performance Node development, then Trevor is someone you should follow.
Run the following commands to clone the repo and to install the necessary dependencies.
The initial source,
concat-files-nested.js, uses nested callbacks.
Nested callbacks can be used effectively. However, as the nesting gets deeper our code moves further and further to the right, which when taken too far, contributes to a condition known as callback hell.
Response time is 290 ms.
Nested callbacks with error
When we run
concat-files-nested-with-error.js the error message displayed in the console is:
Notice how we get an error message that shows the exact line number where the error occurs. However, there is no information about which callback function the error occurred in as we're using anonymous functions for our callback.
Importantly, we'll see how using named functions results in improved error messages below.
Unnest a series of callbacks
Unnested callbacks yield a better response time.
Response time is 114 ms.
Pass data via file globals
The first example shows unnested callbacks where data is passed by using file globals (basically variables that are defined outside of function scope). To unnest we made the following changes to the original
- Create 3 file global variables: file1Contents, file2Contents, file3Contents
- Name each callback function
- Unnest the callback
- Set the value of each file global with the appropriate
datawithin each function that reads a file's contents
Pass data via properties
In this example we'll pass the return values by attaching prior return values as properties on functions that will be called further down our file.
The advantage of this approach is that we avoid the use of any global variables in our file.
Alternative approaches to passing data
There are additional methods to pass data from one callback to another including the use of closures. However, I am not going to cover this alternatives in this repo.
Unnested callbacks with error
When we run
concat-files-unnested-pass-via-properties-with-error.js the error message displayed in the console is:
By using named functions we've given additional information to Node which helps it give us a better error message. Notice how Node is able to report the line number where the error occurred
31 and the exact error
ReferenceError: errWrong is not defined, and it also reports the exact function that contains the error
Nested method declarations
The original of this approach is from Trevor Norris. You can view his original slides at http://trevnorris.github.io/NodeDay/#/13.
Instantiates in 280 ns (that's nano seconds).
Unnested method declarations
Instantiates in 7 ns (that's nano seconds).