I have an array of Elements, and each element has a property :image.
I would like an array of :images, so whats the quickest and least expensive way to achieve this. Is it just iteration over the array and push each element into a new array, something like this:
images = []
elements.each {|element| images << element.image}
You can use the Benchmark module to test these sorts of things. I ran @sepp2k’s version against your original code like so:
The output on my machine was consistently (after more than 3 runs) very close to this:
Thus demonstrating that
mapis significantly faster than manually appending to an array when the array is somewhat large and the operation is performed many times.