r/javascript • • Feb 20 '18

Optional Chaining may be coming to JavaScript

https://dev.to/sammyisa/optional-chaining-may-be-coming-to-javascript--4ff0
321 Upvotes

100 comments sorted by

View all comments

1

u/DukeBerith Feb 20 '18

Cool!

For anyone wanting to use this today and is already using lodash or underscore, use the _.get method

_.get(object, 'path.to[0][1].deeply.nested.freakin.item', optionallyDefaultValue)

https://lodash.com/docs#get

4

u/bart2019 Feb 20 '18

Meh.

That means the code has to parse the path, every single time, and then, traverse it.

1

u/DukeBerith Feb 22 '18

Nah.

Lodash's stuff tends to be memoised, and a quick skim in the source code shows that the path gets computed once and stored for future reuse. It only is parsed every time if that path is dynamic and is different every single time.

1

u/bart2019 Feb 22 '18

It still needs a loop.