IIRC the stringify/parse solution is often used for performance reasons. Recursively copying nested properties from objects can be slow...
As already suggested you can look at the lodash implementation, but that will have been written for performance and not legibility.
I am a believer of the Root Cause Analysis. Ask "How, What, Why" until you can dissect it with no further to go. Well, I am a frontend developer ready for new challenges!
Hi Ben,
Thanks for bringing the performance issue in to the picture. This is obviously an important concern.
Although JSON.stringify solved 98% of the use-case. There is the 2% that I am targeting in this discussion:
1- Conservation of data not identified by JSONing. Like:
JSON.stringify({key:Nan});JSON.stringify({key:Infinity});// all will be converted to "{"key": null}"
2- Cyclic dependency.
Could you point me to Lodash's implementation if you have it hand? ( PS- I am not being lazy, i will google it anyways, but would like to be aware of whether i am at the right source :D ) Thankyou!!
...and you'll discover why the usual suggestion is to not reinvent the wheel :D
TBH I'd only bother looking into a custom solution if lodash doesn't offer adequate performance and the stringify/parse approach isn't appropriate... That's not something I've had to worry about so far 🤷
I am a believer of the Root Cause Analysis. Ask "How, What, Why" until you can dissect it with no further to go. Well, I am a frontend developer ready for new challenges!
Hi Ben,
I did take a look at the lodash implementation. And lord that's one hell of an source code! Also tried some suggestions by Dong, but looks like when things come to cyclic dependencies and a lot of corner cases, life isn't as simple!
And yes, it makes sense to only to bother about customizing the implementation when neither of the available solutions are performing as required. Thanks again! :D
P.S. It's going to take a while for me to make peace of Lodash's deep clone source code! 😂
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
IIRC the stringify/parse solution is often used for performance reasons. Recursively copying nested properties from objects can be slow...
As already suggested you can look at the lodash implementation, but that will have been written for performance and not legibility.
Hi Ben,
Thanks for bringing the performance issue in to the picture. This is obviously an important concern.
Although JSON.stringify solved 98% of the use-case. There is the 2% that I am targeting in this discussion:
1- Conservation of data not identified by JSONing. Like:
2- Cyclic dependency.
Could you point me to Lodash's implementation if you have it hand? ( PS- I am not being lazy, i will google it anyways, but would like to be aware of whether i am at the right source :D ) Thankyou!!
So you could look at: github.com/lodash/lodash/blob/mast...
...and you'll discover why the usual suggestion is to not reinvent the wheel :D
TBH I'd only bother looking into a custom solution if lodash doesn't offer adequate performance and the stringify/parse approach isn't appropriate... That's not something I've had to worry about so far 🤷
Hi Ben,
I did take a look at the lodash implementation. And lord that's one hell of an source code! Also tried some suggestions by Dong, but looks like when things come to cyclic dependencies and a lot of corner cases, life isn't as simple!
And yes, it makes sense to only to bother about customizing the implementation when neither of the available solutions are performing as required. Thanks again! :D
P.S. It's going to take a while for me to make peace of Lodash's deep clone source code! 😂