Skip to content

Instantly share code, notes, and snippets.

@AndyDangerous
Created March 31, 2022 20:01
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 AndyDangerous/79c37314dc99b63edc92399ab85d9ea8 to your computer and use it in GitHub Desktop.
Save AndyDangerous/79c37314dc99b63edc92399ab85d9ea8 to your computer and use it in GitHub Desktop.
Faker vs. constant performance benchmarks
require 'benchmark'
require 'faker'
require 'securerandom'
n = 5000
Benchmark.bm(26) do |x|
NUM = 123.freeze
PASSWORD = "Password!1".freeze
UUID = SecureRandom.uuid.freeze
RAND = 1000.freeze
x.report("faker number:") { n.times do ; Faker::Number.number(digits: 3); end }
x.report("constant number:") { n.times do ; NUM; end }
x.report("faker password(6):") { n.times do ; Faker::Internet.password(min_length: 6); end }
x.report("faker password(15):") { n.times do ; Faker::Internet.password(min_length: 15); end }
x.report("faker password(rand(1000)):") { n.times do ; Faker::Internet.password(min_length: rand(1000)); end }
x.report("constant password:") { n.times do ; PASSWORD; end }
x.report("generate UUID:") { n.times do ; SecureRandom.uuid; end }
x.report("constant UUID:") { n.times do ; UUID; end }
x.report("rand(1000):") { n.times do ; rand(100); end }
x.report("constant 1000:") { n.times do ; RAND; end }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment