creator-docs icon indicating copy to clipboard operation
creator-docs copied to clipboard

New DeepClone code snippet

Open EnumEnv opened this issue 1 year ago • 5 comments

Changes

I've made a bit more documented of a code snippet to help scripters navigate their way through it, considering not everyone viewing would be experienced. This take on the deep clone snippet also gives a faster result, meaning the code is ran faster and you get your value returned faster.

OLD: 0.0000051 (os.clock) NEW: 0.0000034 (os.clock)

The difference may be small but definitely in some cases noticeable and useful. This is due to the usage of the generalized iterator. Also simplifying the code with ternary operators. But, I believe my take on this new snippet would definitely be faster, and also educate other scripters better, even spiking their curiosity in code simplification if not familiar with ternary operators. All in short, I believe It's a great take.

https://create.roblox.com/docs/luau/tables#deep-clones https://devforum.roblox.com/t/ternary-like-operation-in-lua/834254

Checks

By submitting your pull request for review, you agree to the following:

  • [x] This contribution was created in whole or in part by me, and I have the right to submit it under the terms of this repository's open source licenses.
  • [x] I understand and agree that this contribution and a record of it are public, maintained indefinitely, and may be redistributed under the terms of this repository's open source licenses.
  • [x] To the best of my knowledge, all proposed changes are accurate.

EnumEnv avatar Sep 28 '24 20:09 EnumEnv

table.clone would like to have a word with you about performance (unsure if its faster since you still have to iterate the table for tables)

metatablecat avatar Sep 30 '24 01:09 metatablecat

Hi @EnumEnv , in your version, where does MY_deepCopy come into play?

IgnisRBX avatar Oct 01 '24 19:10 IgnisRBX

Hi @EnumEnv , in your version, where does MY_deepCopy come into play? @IgnisRBX Oh, my bad, that's actually a test thing I had set up before for recursion, was using 2 functions for comparing and well forgot to rename. I'll just change it back to the function's name "deepCopy". That's my bad

EnumEnv avatar Oct 02 '24 14:10 EnumEnv

Using generalised iteration (for k, v in o do) and shorthand if statements is faster, my table.clone idea is a lot slower lol.

image

The code is provided within the image. Test sample is a table with 10,000 entries with empty tables randomly intersperced.

metatablecat avatar Oct 03 '24 00:10 metatablecat

@metatablecat The reason the 'your solution' one is slower is because you used "pairs", which I initially didn't use at all, I used the generalized iteration. So, that's the reason of it slowing down. Generally it should be faster.

EnumEnv avatar Oct 05 '24 06:10 EnumEnv

Waiting on another review. Tested everything and re-benchmarked.

EnumEnv avatar Oct 05 '24 19:10 EnumEnv

@EnumEnv looks good now! I just did minor edits to the comment lines (no ending periods as per our typical style, etc.)

IgnisRBX avatar Oct 07 '24 15:10 IgnisRBX