Last Updated: February 25, 2016
·
2.087K
· nick-desteffen

Lower your cost for speedy tests when using BCrypt

If you are using BCrypt Ruby you can have some significant time savings when running your unit tests by lowering the cost. By default BCrypt::Password.create(unencrypted_password) will use a cost of 10. I dropped a RSpec test suite from 25 seconds to just 4 by just setting the cost to be the minimum (4) in test mode.

def password=(unencrypted_password)
  @password = unencrypted_password
  unless unencrypted_password.blank?
    if Rails.env.test?
      cost = BCrypt::Engine::MIN_COST
    else 
      cost = BCrypt::Engine::DEFAULT_COST
    end
    self.password_digest = BCrypt::Password.create(unencrypted_password, cost: cost)
  end
end

1 Response
Add your response

Nice tip!
You can also set it on your test_helper.

over 1 year ago ·