I just looked at compiled script of my source code and I think I noticed something weird.
This ES6 code:
const data = {someProperty:1, someNamedPropery: 1, test:1};
const {
someProperty, // Just gets with 'someProperty' peoperty
someNamedPropery: changedName, // Works same way, but gets with 'changedName' property
test: changedNameWithDefault = 1, // This one, introduces new variable and then uses it to compare that to void 0
} = data;
Compiles to this:
"use strict";
const data = {someProperty:1, someNamedPropery: 1, test:1};
var someProperty = data.someProperty,
changedName = data.someNamedPropery,
_data$test = data.test,
changedNameWithDefault = _data$test === void 0 ? 1 : _data$test;
I am curious why Babel introduces new variable _data$test. Can't it just be something like this?
...
changedNameWithDefault = data.test === void 0 ? 1 : data.test;
It still works.
Notice, that new variable introduction happens only when I'm trying to assign default value if key isn't present in data variable or is undefined.
Does this affect application performance if data.test is big enough? And I wonder if Garbage Collector takes care of it (_data$test variable).