Skip to content

Instantly share code, notes, and snippets.

@netshade
Created July 8, 2014 14:44
Show Gist options
  • Select an option

  • Save netshade/a5fe9d611e9dec1e4901 to your computer and use it in GitHub Desktop.

Select an option

Save netshade/a5fe9d611e9dec1e4901 to your computer and use it in GitHub Desktop.
Object Copy Times
# Ruby 2.1, Macbook Pro 2.6 i7, 16GB RAM
# Only 1 iter, I know i know i know
irb(main):001:0> require 'benchmark'
=> true
irb(main):002:0> h = {}
=> {}
irb(main):003:0> 1000_000.times.map { |i| h[i] = i }; nil
=> nil
irb(main):004:0> Benchmark.realtime { h.dup }
=> 0.864854
irb(main):005:0> a = 1_000_000.times.map { |i| i }; nil
=> nil
irb(main):006:0> Benchmark.realtime { a.dup }
=> 0.002181
// Node v0.10.2, Macbook Pro 2.6 i7, 16GB Ram
// Only 1 iter
-> % node
> a = []
[]
> h = {}
{}
> for(var i = 0; i < 1000000; i ++){ a.push(i); h[i] = i; }
999999
> var benchmarkRealtime = function(fn){ var t = new Date().getTime(); fn(); return (new Date().getTime() - t) / 1000.0; }
undefined
> benchmarkRealtime(function(){ var newArray = a.slice(); return newArray; })
0.022
> function clone(o) {
... var ret = {};
... Object.keys(o).forEach(function (val) {
..... ret[val] = o[val];
..... });
... return ret;
... }
undefined
> benchmarkRealtime(function(){ var newObject = clone(h); return newObject; })
0.442
> benchmarkRealtime(function(){ var newObject = JSON.parse(JSON.stringify(h)); return newObject; })
0.705
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment