New V8 coming soon and node. js

Source: Internet
Author: User

node. JS relies on the V8 engine and the V8 engine is a JavaScript virtual machine written by Google for the Chrome browser. From the beginning, V8 's main goal was to make JavaScript run faster, or at least faster than its competitors. This is not easy for high dynamics. This section is about the evolution of V8 and JS engine performance.

The core part of the V8 engine is the JIT (Just in time) compiler capable of executing JavaScript at high speed. This is a dynamic compiler that can optimize code at run time. When V8 was originally built, the JIT compiler was called Fullcodegen. The V8 team then implemented the crankshaft, which included many Fullcodegen performance optimizations that were not implemented.

Starting in the 90 's, JavaScript is often difficult to understand which operations are fast and which are slow (whatever the engine is), so the reason for the slow JavaScript code is usually hard to comprehend.

In recent years, Matteo Collina and I have focused on how to write node. js code with excellent performance. Of course, this means that when the V8 engine executes the code, you know which methods are fast and which are slow.

Now it's time for us to challenge all the performance assumptions, because the V8 team has written a new JIT compiler: turbofan.

Starting with the common "V8 killer", to the more complex Matteo and my discovery of crankshaft performance, we will evaluate V8 based on a series of test results.

Of course, before we optimize the V8 code, we should focus on API design, algorithms and data structures (premature optimization is the root of all evils). Several versions of Node are tested for performance below. We can get some conclusions to influence the style of our code, and how to improve performance after applying the usual optimizations.

Try/catch problems

A very well-known optimization mode is to use Try/catch blocks.

Test code links (https://github.com/davidmarkclements/v8-perf/blob/master/bench/try-catch.js) test the following 4 scenarios, respectively

There are try/catch in the function
No try/catch in function
Calling a function in try
Call function (no try/catch)

Test data

?

We can see that the performance problem of Try/catch in node 6 (V8 5.1) does exist, but there is a noticeable improvement in node 8 to node 8.2.

It is also worth noting that calling a function in a try block is significantly slower than it is outside the try block.

The effect of Try/catch on the calling function is negligible for versions above node 8.3.

Delete an object's properties

Property lookups are more complex because of the dynamic nature of JavaScript objects and the prototype chain.

There are 3 ways to delete object properties (test code https://github.com/davidmarkclements/v8-perf/blob/master/bench/property-removal.js).

Set direct to object property to undefined by setting
Delete Properties via delete
To delete recently added properties

?

In turbofan (V8 6.0 and 6.1) it can be found that removing the most recently added attribute is more efficient and is also very efficient by setting the object properties directly to undefined. The delete operation is poorly performed in most cases.

Arguments

The normal JavaScript function can be an implicit parameter object, which is much like an array.

In order to use the array method or most array behavior, the indexed property of the parameter object has been copied into the array. In the past, people in JavaScript tended to equate less code with faster code. While this rule of thumb brings the benefits of payload to browser-side code, the same rules cause the server-side code to be much less painful than its execution speed. Therefore, a concise way to convert a parameter object to an array becomes very popular:

Array.prototype.slice.call (arguments). This calls the array slice method to pass the parameter object as the context of the method, and the slice method obtains an array-like object and executes accordingly. That is, it takes the entire parameter Array-like object as an array.

However, when an implicit parameter object of a function is exposed from the context of a function (for example, when it is returned from a function or passed to another function, as shown in Array.prototype.slice.call (arguments)), this usually results in performance degradation.

Test code (HTTPS://GITHUB.COM/DAVIDMARKCLEMENTS/V8-PERF/BLOB/MASTER/BENCH/ARGUMENTS.JS)

Exposing arguments objects to other functions
Copying arguments objects using Array.prototype.slice
Use a loop to copy each property of an object
Using the ES6 expansion operator

?

Let's turn this data into a line chart:

?

If we want to write the arguments of the function input into an array, we recommend using the spread operator for the above Node 8.3 version. In Node 8.2 and below we'd better improve performance by looping through the object properties to a new array.

For node 8.3 and above, it is good performance to expose parameter objects directly to other functions, so it can greatly improve the efficiency of code execution without having to write the parameters into an array.

New V8 coming soon and node. js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.