Designly
Mar 11, 2022

--

It really depends on what this code represents in a larger context. Is this a library of the names of baby animals? If so, using an object is the correct approach. But what if you wanted a default case? I think a switch or if/else if block would be more suitable. I use switch statements all the time. They are very useful for finding a case in a single variable and can be matched against a regular expression as well.

Also, an object stored in memory may consume more memory, but hash tables are an intrinsically faster method to search through large stores of key-value pairs. If you were to, for example, use this code to find the baby animal name for every animal on Earth, one would almost certainly use a hash table, or better yet, an SQL database.

--

--

Designly
Designly

Written by Designly

Full-stack web developer and graphics designer.

No responses yet