Consider a scenario where an object contains keys that can hold either a single value or an array of values.
For example:
sample = {}
sample[key] = "foo"
sample[bar] = ["Alpha", "Bravo", "Charlie"]
When iterating over this object (sample
) to output the values, a decision needs to be made on how to handle values that are arrays.
Which approach is more resource-intensive?
Is it more costly to check if a value is an array every time during iteration:
if(Array.isArray(arr[prop]))
OR
Would it be better to simply make all keys store arrays for easier iteration:
arr[prop].forEach(function(val){}
From an algorithmic standpoint, is allocating additional space to convert keys into arrays worth the ease of iteration?