Clone with better performance

前端 未结 8 2387
一整个雨季
一整个雨季 2020-12-14 04:29

I want to create deep copy method and I found 3 ways to execute it

1-deep copy with pass each property 1 by 1

2-using reflection

3-using serializatio

8条回答
  •  难免孤独
    2020-12-14 04:46

    The first option, manually deep copying your values, will be the most performant by far.

    Reflection will introduce quite a bit of overhead, as it is (relatively) slow to access data.

    Serialization is adding a huge cost, as it serializes the data into a temporary structure, then reverses the process to set. This is again, very slow.

    The only advantage to option 2 or 3 is that its potentially easier to implement, and reusable across multiple types. The first option has to be hand-written per type, but is much faster (and more efficient in memory usage than option 3, as well).

提交回复
热议问题