Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5999043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:29:45+00:00 2026-05-23T00:29:45+00:00

I know that serializing an object is (to my knowledge) the only way to

  • 0

I know that serializing an object is (to my knowledge) the only way to effectively deep-copy an object (as long as it isn’t stateful like IO and whatnot), but is one way particularly more efficient than another?

For example, since I’m using Rails, I could always use ActiveSupport::JSON, to_xml – and from what I can tell marshalling the object is one of the most accepted ways to do this. I’d expect that marshalling is probably the most efficient of these since it’s a Ruby internal, but am I missing anything?

Edit: note that its implementation is something I already have covered – I don’t want to replace existing shallow copy methods (like dup and clone), so I’ll just end up likely adding Object::deep_copy, the result of which being whichever of the above methods (or any suggestions you have 🙂 that has the least overhead.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T00:29:46+00:00Added an answer on May 23, 2026 at 12:29 am

    I was wondering the same thing, so I benchmarked a few different techniques against each other. I was primarily concerned with Arrays and Hashes – I didn’t test any complex objects. Perhaps unsurprisingly, a custom deep-clone implementation proved to be the fastest. If you are looking for quick and easy implementation, Marshal appears to be the way to go.

    I also benchmarked an XML solution with Rails 3.0.7, not shown below. It was much, much slower, ~10 seconds for only 1000 iterations (the solutions below all ran 10,000 times for the benchmark).

    Two notes regarding my JSON solution. First, I used the C variant, version 1.4.3. Second, it doesn’t actually work 100%, as symbols will be converted to Strings.

    This was all run with ruby 1.9.2p180.

    #!/usr/bin/env ruby
    require 'benchmark'
    require 'yaml'
    require 'json/ext'
    require 'msgpack'
    
    def dc1(value)
      Marshal.load(Marshal.dump(value))
    end
    
    def dc2(value)
      YAML.load(YAML.dump(value))
    end
    
    def dc3(value)
      JSON.load(JSON.dump(value))
    end
    
    def dc4(value)
      if value.is_a?(Hash)
        result = value.clone
        value.each{|k, v| result[k] = dc4(v)}
        result
      elsif value.is_a?(Array)
        result = value.clone
        result.clear
        value.each{|v| result << dc4(v)}
        result
      else
        value
      end
    end
    
    def dc5(value)
      MessagePack.unpack(value.to_msgpack)
    end
    
    value = {'a' => {:x => [1, [nil, 'b'], {'a' => 1}]}, 'b' => ['z']}
    
    Benchmark.bm do |x|
      iterations = 10000
      x.report {iterations.times {dc1(value)}}
      x.report {iterations.times {dc2(value)}}
      x.report {iterations.times {dc3(value)}}
      x.report {iterations.times {dc4(value)}}
      x.report {iterations.times {dc5(value)}}
    end
    

    results in:

    user       system     total       real
    0.230000   0.000000   0.230000 (  0.239257)  (Marshal)
    3.240000   0.030000   3.270000 (  3.262255)  (YAML) 
    0.590000   0.010000   0.600000 (  0.601693)  (JSON)
    0.060000   0.000000   0.060000 (  0.067661)  (Custom)
    0.090000   0.010000   0.100000 (  0.097705)  (MessagePack)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that I can do something like $int = (int)99; //(int) has a
I know that you can insert multiple rows at once, is there a way
I have a object that im serializing to a JSON object using JSON.Net. This
I know that might sound like a stupid question since it's a trivial feature
I know that default cron's behavior is to send normal and error output to
I know that |DataDirectory| will resolve to App_Data in an ASP.NET application but is
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
I know that .NET is JIT compiled to the architecture you are running on
I know that the following is true int i = 17; //binary 10001 int
I know that just using rand() is predictable, if you know what you're doing,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.