“Better” is a subjective term.
Some points to consider:
- Yes, if you had a
Map
or an object keyed byid
for patients, beds, and departments, in general looking up patients, beds, and departments byid
would be faster using thatMap
/object instead of a linear searches of those arrays. - But, presuming that you need the arrays as well for other parts of your code, maintaining both an array and a
Map
/object incurs costs of code complexity (the main issue) and additional overhead. - A linear search will only be problematic if the arrays are large.
If there’s a performance problem with the code listed, then you might consider Map
/object lookup (accepting the costs of adding it in favor of increased speed). But if there isn’t, it may be premature optimization introducing unnecessary code complexity.
8
solved which code is better? [closed]