Blog

# ES6’s Function Destructuring Assignment Is Not A Free Lunch.

I completely agree with the fact that “premature optimization is the root of all evil (or at least most of it) in programming”. But it makes no harm sometimes to know the bits of your code that you write and how it affects the environment for which you are writing your code.

This post is more about learning some of these bits of JavaScript, especially in Node.js environment with v8 core not to favour this micro-optimization because business code is still more concerned about readability and maintainability.

So what exactly we are looking at?. For the legibility of the remaining article, we are comparing the ES6 way of destructing assignment and traditional way of passing parameters inside a function, from a perspective of its impact on performance CPU/Memory.

## Dissecting the performance from the V8 bytecode.

Let’s look at the byte code of function parameters of the below sample code.

ByteCode:

So in the case of V8 have only 2 core instruction to execute Ldar a1 and Add a0,[0].

Let’s us rewrite the above add function using the ES6’s Function Destructuring Assignment.

ByteCode:

In the case of ES6’s Function Destructuring Assignment we can see that the byteCode has increased significantly from 4 to 19 where most of the them are JumpIfUndefined, CallRuntime, Throw instructions.

## Dissecting the performance w.r.t the memory.

Generating too many function call to trace the GC.

function parameters

Result of — trace-gc

[1841:0x2555580] 82 ms: Scavenge 3.4 (6.3) -> 3.1 (7.3) MB, 1.9 / 0.0 ms allocation failure [1841:0x2555580] 102 ms: Scavenge 3.6 (7.3) -> 3.5 (8.3) MB, 1.9 / 0.0 ms allocation failure **4919768**

ES6’s Function Destructuring Assignment.

Result of — trace-gc

[5054:0x245d570] 60 ms: Scavenge 3.4 (6.3) -> 3.1 (7.3) MB, 1.1 / 0.0 ms allocation failure [5054:0x245d570] 77 ms: Scavenge 3.6 (7.3) -> 3.5 (8.3) MB, 1.3 / 0.0 ms allocation failure [5054:0x245d570] 109 ms: Scavenge 4.8 (8.3) -> 4.6 (11.3) MB, 0.5 / 0.0 ms allocation failure **5762568**

But does it means in ES6’s Function Destructuring Assignment we have more memory allocation, NO as here most of the memory difference is due to the object creation overhead in each loop.

Because if we try to test the ES6’s Function Destructuring Assignment with constant object outside the loop we would’t get much memory difference, from the function parameters.

GC result when object was created only once.

So we have almost the same memory utilization as the functional parameter case.

So in the case of ES6’s Function Destructuring Assignment the memory utilization difference is directly dependent upon how we are creating the object.

Also if we see verbose GC detailed information in the case of const d = add({a:1,b:2}), we will see that most of these allocation happens in new space region, so the cost of these clean-up is still relatively small (<1 ms), but this may impact the performance of the application (depending upon the application use case) because the Garbage Collector in V8 is generational, stop-the-world garbage collector.

How to Read — trace_gc flag node.js

Though we have a evident Computational penalty of using ES6’s Function Destructuring Assignment, the memory penalty depends upon the use case.

At first glance, we might be tempted to do premature optimization but for business we should prefer the readability, and also in this case for the code sample above, we have seen the results, still there is a very high probability that final benchmark will differ from project to project in real world because V8 performs lots of optimization with different mechanism eg. escape analysis on destructuring, and we might never have to worry about this in most of the cases.