I have a super simple function that returns a text label for a range of the input
const labelByAmounts = [
{ min: 1, max: 10, label: 'small' },
{ min: 11, max: 20, label: 'medium' },
{ min: 21, max: 30, label: 'large' },
]
function fn2(amount) {
for (const labelByAmount of labelByAmounts) {
if (amount >= labelByAmount.min && amount <= labelByAmount.max)
return labelByAmount.label
}
}
I wonder what the time complexity of this function is? is it o(1)
since the computation doesn’t grow linearly with the growth of input size?
And this is an over-engineered version of the function
const labelByAmounts = [{min: 1, max: 10, label: 'small'}, {min: 11, max: 20, label: 'medium'}, {min: 21, max: 30, label: 'large'}]
function fn2(amount) {
for(const labelByAmount of labelByAmounts) {
if(amount >= labelByAmount.min && amount <= labelByAmount.max) return labelByAmount.label
}
}
Now I wonder what the time complexity of this function is? is it still o(1)
or it is actually o(n)
since there is a loop involved?