I'm building a large hash (~300,000 keys and values). I'm wondering if the read/write processing time of the hash will change if I have human readable key names, like: :some_description_of_the_key
which some are around 30 characters.
Or would there be an advantage to shortening the keys to a sequenced number system (:0_1
, :1_1
as a rudimentary example)?
The hypothetical advantage being that each key would have a much shorter character length.
In short, it would reduce the size but is unlikely to improve performance assuming your key is currently a string, number or symbol and not an object. If your key is a model or other object I recommend changing this as it does add overhead. If we’re talking about the difference between:
{ my_very_long_key_or_something: “”}
Vs
{ 1447 => “” }
Then you’re saving a number of bytes equal to the number of character reduced. So for 300k records, a saving of 15 characters each would be 4.29Mb. If you’re dealing with low memory and that kind of saving is a benefit then go for it. I really recommend revealing keys to make it understandable though.
Again, provided your key isn’t an object; your read/write issues are more likely related to the size of the value (for objects or nested hashes) or the processing you’re applying to the hash. You can try benchmarking to compare performance with:
Benchmark.ms { my_hash.process }