BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News V8 JavaScript Engine 8.0 Reduces Heap by 40%, Adds Optional Chaining and Null Coalescing

V8 JavaScript Engine 8.0 Reduces Heap by 40%, Adds Optional Chaining and Null Coalescing

This item in japanese

Bookmarks

The latest release of Google's V8 JavaScript engine, V8 8.0, uses pointer compression to reduce heap size by 40% and with no performance hit. Additionally, it adds support for optional chaining using the ?. operator and for nullish coalescence using ??. V8 v8.0 will be officially available with Chrome version 80.

V8 v8.0 applies compression to JavaScript tagged values, which are used to represent pointers into the heap or small integers, explains V8 core team member Leszek Świrski. Instead of using the full 64 bits required to represent a pointer on a 64-bit CPU, V8 will only use the lower bits and synthesize the higher bits. The V8 team has fully documented their approach to pointer compression, which leverages the same technique as used by other platforms, including Java. In conversation with InfoQ, Świrski clarified V8 memory compression in V8 v8.0 works by chopping off the top 32 bits from memory addresses. This forces "compressed" pointers to span across a 4GB space, and all "compressed" pointers are used as relative offsets within it. Full pointers are calculated by adding the base offset back to compressed pointers. He also added the team plans to combine word alignment and address-level bit shifting to extend compressed heap size beyond the 4GB limit. The general idea is you conceptually organize your memory into words instead of bytes. If you use 8-byte words, you will only need to represent addressed starting at locations 0, 7, 15, 23, etc, thus giving address spanning 2<sup>3</sup> * 2<sup>32</sup> bytes. 

Significantly, says the V8 team, pointer compression does not exact a performance cost. This is related to the fact that going from a compressed pointer to a full pointer is a rather fast operation in itself. In V8's case there is an additional bonus, which is the garbage collector becoming faster, too. This makes V8 v8.0 actually faster on real web sites such as Facebook, CNN, and Google Maps both on mobile and desktop devices, according to preliminary benchmarks.

On the JavaScript side, V8 v8.0 introduces support for two useful syntax conventions: optional chaining and nullish coalescence.

Optional chaining aims to make it easier to access properties in sequence without incurring the risk of getting an exception because some intermediate object is null or undefined. For example, to prevent the possibility of such an error occurring, in the following code we check in advance that all intermediate properties we are going to access are well-defined:

if (resource && resource.address && resource.address.types)
  return resource.address.types.length

This can be replaced through the following code, where we use the optional chaining operator ?. to make sure the overall expression is short-circuited to undefined as soon as an intermediate component is null or undefined:

return resource?.address?.types?.length

The nullish coalescence operator, ?? is a refinement of || when used in the following context:

let iterations = settings.iterations || 4;

The disadvantage of || in such context is it cannot be used when the value you want to set, in the above example settings.iterations, evaluates to false, e.g., when settings.iterations == 0. In this case you would still end up using the default value, e.g. 4. On the contrary, the nullish coalescing operator ?? will correctly handle such cases, i.e.:

let iterations = settings.iterations ?? 4;

In other words, a ?? b evaluates to b only when a is null or undefined, otherwise it evaluates to a.

V8 v8.0 is not yet the official stable V8 release and will ship in a few weeks in Chrome 80 stable. In the meantime, you can access it using git checkout -b 8.0 -t branch-heads/8.0.

UPDATE 12/23/19: Added Leszek Świrski's clarification about the algorithm for pointer compression actually used in V8.

Rate this Article

Adoption
Style

BT