rails/rails — pull request #17245 by sferik (2014-10-13 11:13:32+00:00)
Title: Replace Array#shuffle.first with Array#sample

Similar to https://github.com/rails/rails/pull/17099, `Array#shuffle` allocates an extra array. `Array#sample` indexes into the array without allocating an extra array. This is the reason why `Array#sample` exists. It is about over 15X faster.

###### Benchmark
```ruby
require 'benchmark/ips'

ARRAY = (1..100).to_a

def slow
ARRAY.shuffle.first
end

def fast
ARRAY.sample
end

Benchmark.ips do |x|
x.report('slow') { slow }
x.report('fast') { fast }
end
```

###### Ruby 2.1.2

```
slow 313591.5 (±12.6%) i/s - 1538366 in 5.025843s
fast 4937855.9 (±10.6%) i/s - 24400316 in 5.018654s

```