Comments (10)
i did a bit of research on this problem. conclusion: there is not a good solution for cloning an object in javascript. there are a number of techniques that make different compromises. For our impl, I think we want:
- detect/avoid cycles. This rules out JSON approach. But without weak maps, this makes the cloning algorithm a nasty O(n^2). With weak maps, could get O(n log n) I believe, which is a big difference.
- include functions (this again rules out JSON approach)
- copy Date objects
after that, there's a lot of squishiness. This problem really brings the limitations of the language into relief.
- what about custom constructed objects? re-construct them on the copy?
- what about primitive wrappers (e.g. Boolean, Number, Striung, etc.)? re-construct them?
- what about
prototype
properties? ignore? - .... more ....
Conclusion: I am going to defer crossing this Rubicon until I am pushed into it. If anyone wants to suggest an implementation, or even submit a pull request with a candidate implementation, I am all ears.
from ramda.
removed cloneDeep
.
from ramda.
We'll think about this a bit.
I don't think it's possible for us to use the structured cloning algorithm directly. That's for language implementors, and depends upon having access to internal Object implementation details.
Ramda has so far been mostly used for working with data objects, so this hasn't been an issue. I can see how this might be a problem. Are there any particular deep-clone-in-JS implementations you particularly admire that we could look to for inspiration should we choose to go in that direction?
from ramda.
I was thinking more like "follows the structured cloning algorithm where possible". e.g. allow Date, RegExp, etc.
http://lodash.com/docs#cloneDeep should offer some ideas.
While a new R.cloneDeep
behavior would be nice, it's most important the behavior is accurately detailed in the docs. If it's using JSON, the docs should include examples illustrating the omission of certain properties.
from ramda.
mea culpa. I put this quick-and-dirty impl in there to support a little project i was toying with. I didn't want to write a type-checking object traversal so I just slapped this in there. I agree with @CrossEye, it would be nice to use an existing deep copy implementation.
from ramda.
@buzzdecafe @CrossEye No worries, I totally understand. "Make it Work. Make it Right. Make it Fast.", "YAGNI", etc, etc.
I just added this to capture the primary issue that the docs ("Create a deep copy of an object") don't match the behavior. There are many ways to resolve it; I just didn't want the issue to get lost.
from ramda.
@jfsiii understood, and agreed.
from ramda.
I totally agree. That's why I jumped to see how Ramda chose to implement it. Choice 1, rename the function, still remains an option.
As long as the docs match the behavior, I'm fine with closing this.
from ramda.
Deep cloning is something everyone does a little differently, so I think it makes sense to leave it out of the library.
I'd be in favor of renaming or removing the function.
from ramda.
@saikobee, i think removing cloneDeep
may be the smartest move.
from ramda.
Related Issues (20)
- [Bug] groupBy result keys order when 0 as first character HOT 6
- R.pipe(R.tryCatch, ...) anomaly HOT 12
- clone does not clone error instance HOT 2
- swap does not work as intended
- Ramda is 3x slower because it uses the arguments object? HOT 10
- Broken link in the "Install specific functions" section of README
- how to implement insertWhile HOT 2
- R.aperture returns arbitrary/random ordering of "consecutive" elements HOT 1
- 0.29.1 Upgrade Guide HOT 11
- compose does not work with zipwith HOT 2
- R.without broke backwards compatibility on #2603 HOT 1
- update vulnerable dependencies
- update sanctuary
- bug: code coverage failing
- enhancement: update prop to include deeply nested objects
- enhancement: update path to use prop
- Support promises for pipe and pipeWith HOT 2
- illegal invocation HOT 4
- `groupBy` throws inscrutable errors when inferred to be a transducer HOT 4
- Incorrectly implementation of the 'unless' function HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ramda.