Also these days there's BigInt.
As in, it’s amazing that anything actually works given the internal inconsistencies in its core.
(Disclaimer: I code ts/js for a living.)
Chrome dev tools logs a pointer to the object, so if the object is mutated after logging, but before you view/expand it (especially for nested structures), it can be very confusing! JSON.parse(JSON.stringify()) usually is good enough for this :)
But yes, generally speaking I think a deep clone is a sign of a smell elsewhere. That doesn't mean it never has a place in any code, but just that if I find myself reaching for it, I usually see if there's a mistake being made elsewhere.
So if between two logs it was modified, it reflects the new status on the log before as well.
I think you may have been confused and got how it is, vs how it ought to be the wrong way round.
https://twitter.com/justinvincent/status/1714866433426067573
Yes. That's why you need to use structuredClone. If you don't, and you mutate the object, then you'll see the new values when you expand the object.
Sure. A library accepts an object as function argument (for example ‘params’) and then uses it inside. Modifying users object in-place would be a bad idea.
For example, I create a revision of a store (a deep clone) and apply the undo frames one-by-one.
Each undo frame has the setter name and its arguments. IOW, the frames contain what changed, such as:
[
[BASE.setCard, 'some-id', CF.title, 'The New Title'],
…more frames
]
Here's a post with more detailshttps://blog.uidrafter.com/architecture-of-a-desktop-alike-s...
Hierarchical data structures, in mindmaps, menu structures, block based notes apps, etc.
Communicating with workerthreads.
I so much would like to pass references of big readonly objects towards them - but no, full copy it is, for every thread.
[0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
(but I hope I missunderstood something there)
But I need many threads to access the same data, at the same time (readonly, so no racecondition).
You might be mixing ArrayBuffer (which is a transferrable object[0] and gets zero-copy moved) and SharedArrayBuffer.
From [1]:
> The structured clone algorithm accepts SharedArrayBuffer objects and typed arrays mapped onto SharedArrayBuffer objects. In both cases, the SharedArrayBuffer object is transmitted to the receiver resulting in a new, private SharedArrayBuffer object in the receiving agent (just as for ArrayBuffer). However, the shared data block referenced by the two SharedArrayBuffer objects is the same data block, and a side effect to the block in one agent will eventually become visible in the other agent.
[0] https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers...
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
No. And I hope to be wrong.
I will give it another try then, but I was using SharedArrayBuffer and I was getting explicit errors about this limitation.
Caveat is you need these headers for security reasons (hence why the sandbox above has Express) or SharedArrayBuffer is not even defined (at least in Firefox):
Cross-Origin-Opener-Policy: same-origin
Cross-Origin-Embedder-Policy: require-corp[0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Deserializing requires paying all the same costs as deepcopying, both in terms of compute time and in terms of memory usage, but it also incurs the extra costs of parsing.
I'm not saying that there wouldn't be benefits to having a serializer for their gamestate (because there would surely be benefits), but I just cannot understand this as an alternative for the problem they described.
The full idea is implement a deckbuilding game like magic and use genetic algorithms and montecarlo simulation to balance the card pool. But that requires millions of games with millions of simulations to work. So i'll see if it's even possible.
For inspiration, many chess engines store game state in a "bitboard" [0] which is a 64 bit representation of some (partial) state of the board.
Of course, at this point you might not even want to be using JavaScript... maybe you could write the hot path in Rust, compile it to wasm, and then use JS for orchestration/UI.
The issue is I want to figure out if there are combos that I don't even know exist that are unusually strong. Basically make sure I balance the card pool but automatically.
Yeah :) Definitely better to make it slow but ship it. Then make it fast later.
First, many "everything must be immutable" libraries provide data structures that give operations that feel like mutations (but actually are not), and many of those can re-use all the old objects that are not mutated. This could save a lot, relative to full naive deepcopies. Or it could save very little, depending on what you're doing.
Second, if you have the ability, you can always mutate before recursion and then revert when the recursion finishes. This could potentially be very tricky, because it's not always easy to revert mutations. As such, it's not a good plan for a prototype, but if you can get it right, this could make your tree search wayyyyy faster.
If an object is immutable and you want to copy/mutate one item it then you can surgically clone it in O(how deeply nested the item to change is). Object spread being the simple case.
JS makes this messy to do though! Lots of spreads and array maps or you pull in a library like Immer and now have “coloured” objects. Immutability in JS is not idiomatic so for that reason I try to avoid unless I really need to.
I could do some of the changes in place, but to optimize Vue reactivity I work on copies.
I use prototypes so only lodash works for me.
I have never needed to "deep clone" an object in JavaScript. Any time I thought I needed to do that, it was actually a code smell for a different underlying problem.
I was glad to see the end of the "immutable everything" dogmatic obsession wrought by Redux. The code smell in that case was usually related to passing entire objects as props.
Can anyone describe a compelling use case for "deep cloning" objects in JavaScript (keep in mind _everything_ is an object in JavaScript) that isn't dancing around some hidden complexity? IMO, if it can't be (de)serialized via JSON.stringify and JSON.parse, then you should consider why you're trying to serialize it in the first place, and why you're scared to pass around the reference to the object. (That said, structuredClone looks like a better alternative to the JSON method - but note it's still not cloning functions, so you should still be careful where/when you use it.)