Skip to content

Instantly share code, notes, and snippets.

@adymo
Last active January 27, 2016 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adymo/a62e502a1ff6afe714e0 to your computer and use it in GitHub Desktop.
Save adymo/a62e502a1ff6afe714e0 to your computer and use it in GitHub Desktop.
Example of each_with_index iterator creating extra objects
GC.disable
before = ObjectSpace.count_objects
Array.new(10000).each do |i|
[0,1].each_with_index do |j, index|
end
end
after = ObjectSpace.count_objects
puts "# of arrays: %d" % (after[:T_ARRAY] - before[:T_ARRAY])
puts "# of extra Ruby objects: %d" % (after[:T_NODE] - before[:T_NODE])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment