Node notes
- Node’s design favors simplicity over complexity
- Node uses familiar POSIX APIs, rather than attempting an improvement
- Node does everything with events, and doesn’t need threads
- Node leverages the existing C libraries, rather than trying to reimplement their
- Node favors text over binary formats
Optimizing our code
- Initially, a first-pass compiler (the full compiler) converts your code into a runnable state as quickly as possible. During this step, type analysis and other detailed analysis of the code is deferred, prioritizing fast compilation – your JavaScript can begin executing as close to instantly as possible. Further optimizations are accomplished during the second step.
- Once the program is up and running, an optimizing compiler then begins its job of watching how your program runs, and attempting to determine its current and future runtime characteristics, optimizing and re-optimizing as necessary. For example, if a certain function is being called many thousands of times with similar arguments of a consistent type, V8 will re-compile that function with code optimized on the optimistic assumption that future types will be like the past types. While the first compile step was conservative with as-yet unknown and un-typed functional signature, this hot function’s predictable texture impels V8 to assume a certain optimal profile and re-compile based on that assumption.
There is no Integer type in JavaScript; there is a Number type defined as a double-precision floating-point number.
Consider the following Node program:
// program.js
let someFunc = function foo(){}
console.log(%FunctionGetName(someFunc));
If you try to run this normally, you will receive an Unexpected Token error – the modulo (%) symbol cannot be used within an identifier name in JavaScript. What is this strange method with a % prefix? It is a V8 native command, and we can turn on execution of these types of functions by using the --allow-natives-syntax flag:
node --allow-natives-syntax program.js
do not ever use delete to remove elements from an array You are simply inserting an undefined value at that position, which is just another way of creating a sparse array
Try not to preallocate large arrays—grow as you go.
Functions containing try-catch constructs are not optimizable, nor are functions containing other unpredictable constructs, like with or eval. If, for some reason, your function is not optimizable, keep its use to a minimum.
A very common optimization error involves the use of polymorphic functions. Functions that accept variable function arguments will be de-optimized. Avoid polymorphic functions.
Unlike var, let is block scoped; it does not apply outside of its containing block:
Another powerful new feature is destructuring, which allows us to easily assign the values of arrays to new variables:
let [executable, absPath, target, ...message] = process.argv;
Arrow functions allow you to shorten function declarations, from function() {} to simply () => {}. Indeed, you can replace a line like this:
Strings are now iterable. Using the new for...of construct, you can pluck apart a string character by character:
Alternatively, use the spread operator:
console.log([...'Mastering Node.js']);
// ['M', 'a', 's',...]