How to find node.js performance optimization killers
How to find Node.js Performance optimization killers
As we all Know , Node.js is based on V8 Javascript Engine. in Node.js, the code is optimized dynamically; this means that the code is optimized according to its runtime behavior.
After reading this post, you should be able to:
Detect if the function is optimized by the JavaScript Engine(V8).
Detect if an optimized function is de-optimized (this can happen as optimization is done dynamically).
Detect why a function cannot be optimized.
this article aims to make those methods available for most Node.js developers so they can be more productive when it comes to optimizing their node.js apps.
Brief Overview of Node.js Performance Optimization in V8 Javascript Engine
Though currently lots of efforts are being taken to make node.js independent from V8 Engine,most node.js apps are based on V8 javascript engine.we will understanding the optimization process for V8 engine in this article.
In V8, the code is optimized dynamically; this means that the code is optimized according to its runtime behavior.
The process occurs during runtime. V8 analyzes the behavior of the code, develops heuristics and proceeds to optimizations based on what it observed.
For instance, V8 spies on the inputs and the outputs of the functions in order to see if it can perform type assertions. If the type of the arguments of a function is always the same, it seems safe to optimize this function from this assertion.
V8 performs diverse cases of optimization, but the one based on the argument's type is probably the easiest to describe.
How Optimization is done.....
Usually, to run this file, we would use the command
$ node index.js
To trace optimizations, we will add an argument to the command line.
the result appears to be ,
The function was marked for recompilation. That is the first step of the optimization of a function.
The function has then been recompiled and optimized.
... followed by a de-optimization
The code is pretty much the same here. But this time, after calling the function with numbers only, we call it with a few strings. It is still a perfectly valid code since the+
operator can be used for number addition and string concatenation.Now running bellow command
gives us,
The first part of this log is pretty similar to the previous paragraph.
However, there is a second part in which the function is de-optimized: V8 detected that the type assumption made before (“inputs of myFunc are numbers”) was false.
Even if JavaScript is not strongly typed, V8 has optimization rules which are. Therefore, it is a good idea to have coherent typings as arguments and return values of a function.
but there are patterns in JavaScript that can have very different behaviors at runtime. V8 decides to never optimize those functions to avoid to fall in a de-optimization hell.
De-optimization hell
De-optimization hell happens in V8 when a function is optimized and de-optimized a lot during the runtime.
After a few cycles optimization/de-optimization, V8 will flag the method as not optimizable. However, a significant amount of time will have been lost in this cycle with impact on the process performances and memory consumption.
Conclusion
In this article, we saw how to trace optimizations, de-optimizations, and non-optimizations in Node.js. This is a good starting point for your journey through optimizing your Node.js code.
A high-level tool to explore optimization and de-optimization is named IRHydra. A short introduction to its usage with Node.js can be found on Eugene Obrezkov’s blog.
Last updated