# Are the hashes faster than activerecord finds?

**URL:** <https://www.ruby-forum.com/t/are-the-hashes-faster-than-activerecord-finds/60540>\
**Category:** Rails\
**Created:** [May 15, 2006, 12:43pm UTC](https://www.ruby-forum.com/t/are-the-hashes-faster-than-activerecord-finds/60540 "2006-05-15T12:43:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [May 15, 2006, 12:43pm UTC](https://www.ruby-forum.com/t/are-the-hashes-faster-than-activerecord-finds/60540/1 "2006-05-15T12:43:29Z")

</div>

Hello, I need to store somewhere one hundred of static ressources text  
field (internationalization).

I wonder if it better to put them in a database or in a hash defined at  
app startup (in environment.rb)

I would go to the second one, but am I right ?

Thanks

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [May 15, 2006, 1:46pm UTC](https://www.ruby-forum.com/t/are-the-hashes-faster-than-activerecord-finds/60540/2 "2006-05-15T13:46:53Z")

</div>

Hashes will be faster. Why not load a hash from a table in  
environment.rb?

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [May 15, 2006, 2:39pm UTC](https://www.ruby-forum.com/t/are-the-hashes-faster-than-activerecord-finds/60540/3 "2006-05-15T14:39:40Z")

</div>

On 5/15/06, Nuno [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello, I need to store somewhere one hundred of static ressources text  
> field (internationalization).
> 
> I wonder if it better to put them in a database or in a hash defined at  
> app startup (in environment.rb)
> 
> I would go to the second one, but am I right ?
> 
> Thanks

The easy answer is: benchmark 🙂

Hashes of reasonable sizes that you keep in memory will be faster than  
just about anything else. So, if there are some keyed pieces of  
information that you use often and all over the place, you indeed  
might want to put them inside a Hash constant loaded through  
environment.rb. The Hash could be either plain Ruby Hash (e.g. a  
constants.rb in /lib, then “require lib/constants” in environment.rb)  
or something you load through Yaml (which is a bit cleaner).

Keeping the Hash in memory would be faster, but there’s a memory cost.  
So if the Hash becomes large, this attitude loses its appeal. Also,  
it’s a good solution for read-only pieces of information, _not_ for  
freely r/w operation (stuff like being thread-safe, locking etc. which  
the database has for saving you from yourself). And if you don’t use  
that Hash that often, consider an alternative or at least think about  
clearing the memory space (by deleting all references to the Hash).

---

<div class="post-metadata">

**Author:** ![nuno](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/65b543/32.png) [@nuno](https://www.ruby-forum.com/u/nuno)\
**Post date:** [May 16, 2006, 12:56am UTC](https://www.ruby-forum.com/t/are-the-hashes-faster-than-activerecord-finds/60540/4 "2006-05-16T00:56:32Z")

</div>

Thank you for your answer, I’ll go the hash way (loaded from the DB at  
app startup)

The hash (which will be read only BTW) will not be that huge so I  
presume that the memory cost will not be to high and will be shared  
accross all http session
