| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
Sorry, something went wrong.
There was a problem hiding this comment.
I think this is great, thank you so much for working on this! Just left one small suggestion.
Sorry, something went wrong.
There was a problem hiding this comment.
excellent, thanks for improving these generators @alextaujenis ! 👏
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Motivation / Background
This Pull Request has been created because the Faker::Crypto methods are generating extraordinarily long random strings to run through each hashing algorithm. The performance of each method within Faker::Crypto can be improved by up to 80% by balancing the complexity of the random strings for each hash algorithm.
Additional Information
The MD5 hash algorithm returns a string of 32 characters within the range of "a - f" and "0 - 9", which is a range of 16 characters. The total number of possible combinations with this setup is 16^32.
The method Faker::Crypto.md5 computes the MD5 hash of a random string like this:
Digging a little deeper we see that Lorem.characters returns a default number of 255 Alphanumeric characters:
Going further we find out that Faker::Alphanumeric.alphanumeric returns random characters within the range of "a - z" and "0 -9", which is a range of 36 characters. The total number of possible combinations with this setup is 36^255, which is mind-bogglingly larger than 16^32 (the total number of possible combinations for the MD5 hash algorithm).
Optimization
The total number of random characters passed through the MD5 hashing algorithm within Faker::Crypto can be safely reduced from 255 down to a specific number. You can find that number by solving for the minimum value of x within: 36^x > 16^32. This allows MD5 collisions to occur before (non-unique) collisions within Faker::Lorem.characters.
We can find that perfect number for each hash algorithm by running this ruby solver:
Which provides this output:
From this output we can see the point at which 36^x > 16^32:
This shows that Faker::Lorem.characters(number: 24) has 317830109213583906223287396307975536640 less possible combinations than the MD5 hash algorithm, while Faker::Lorem.characters(number: 25) has 467998910543825597179764993024768081920 more possible combinations than the MD5 hash algorithm.
We can safely reduce the number of random characters to 25 for the Faker::Crypto.md5 algorithm while still returning deterministically unique hashes. Here are the other optimal values for each algorithm:
Performance Benchmark
You can see from the benchmark below that after reducing library complexity the Faker::Crypto methods are up to 80% faster, while the Faker::Omniauth methods enjoy performance gains up to 50% depending upon how heavily they rely upon the Faker::Crypto methods.
Checklist
Before submitting the PR make sure the following are checked: