GithubHelp home page GithubHelp logo

joesdu / objectcloner Goto Github PK

View Code? Open in Web Editor NEW

This project forked from marcelltoth/objectcloner

0.0 0.0 0.0 72 KB

Insanely fast and capable Deep Clone implementation for .NET based on Expression Trees

License: MIT License

C# 100.00%

objectcloner's Introduction

ObjectCloner

Build status codecov Nuget GitHub

ObjectCloner aims to be the fastest, yet most capable deep (or shallow) clone solution for .NET objects. It utilizes compiled Expression Trees under the hood, making it just as fast as (or faster than) a hand-written DeepClone method.

Features

  • Native speed using code generation
  • Works on any type
  • Does not require a parameterless constructor or custom attribute
  • Clones properties
  • Clones read-only fields
  • Preserves reference equality in the tree1
  • Handles circular dependencies
  • Handles polymorphism2
  • MIT license
  • 100% test coverage

1 For example if you put the same object into an array 3 times, you will get back a different list which also contains the same single clone object 3 times.

2 Interface-typed fields (IEnumerable, etc.) will be cloned using their actual runtime types.

Installation

The package is available from NuGet as ObjectCloner and maybe ObjectCloner.Extensions (see Usage). Install via the package manager console: PM> Install-Package ObjectCloner the dotnet command line: dotnet add package ObjectCloner or using your IDE's visual NuGet tool.

Usage

The library exposes a static class with two generic methods, making the usage very straightforward.

var clone = ObjectCloner.DeepClone(original);

or

var clone = ObjectCloner.ShallowClone(original);

If you would rather use an extension method format and don't mind polluting your object type install the package ObjectCloner.Extensions as well and use it like so:

var clone = original.DeepClone();

Performance

In a benchmark cloning a custom class hierarchy ObjectCloner beats every traditional solution except custom written cloning code.

Method Mean Error StdDev Ratio RatioSD
Custom Code 88.71 ns 4.242 ns 2.826 ns 1.00 0.00
ObjectCloner 520.09 ns 54.632 ns 36.136 ns 5.88 0.56
Reflection 2,648.47 ns 172.854 ns 112.711 ns 29.88 1.51
Newtonsoft.Json 9,315.16 ns 661.012 ns 437.218 ns 105.10 6.05
BinaryFormatter 22,743.60 ns 1,369.405 ns 1,121.254 ns 256.52 10.89

You can find the benchmarks in the samples folder of the solution and run them yourself.

objectcloner's People

Contributors

marcelltoth avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.