Skip to content

Instantly share code, notes, and snippets.

@pierrax
Last active May 31, 2016 16:05
Show Gist options
  • Save pierrax/1b3bdf95e1fa7c61457e85d7f2e86c12 to your computer and use it in GitHub Desktop.
Save pierrax/1b3bdf95e1fa7c61457e85d7f2e86c12 to your computer and use it in GitHub Desktop.
Improve Rails perf

Use pluck instead of map

Celebrity.all.map(&:id)

=> Time: 3.925s

Celebrity.select(:id).map(&:id)

=> Time: 0.098s

Celebrity.pluck(:id)

=> Time: 0.009s

Pluck is 10 times quicker !

pluck directly converts a database result into an array, without constructing ActiveRecord objects.

Use String::<< instead of String::+=

long_string = 'foo' * 10000000
long_string += 'foo'

=> Time: 0.0862s

long_string << 'foo'

=> Time: 0.00003s

Use Interpolation

long_string + long_string

=> Time: 0.20s

"#{long_string} #{long_string}"

=> Time: 0.04s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment