Is there a fast way to get the minimum and maximum value among the properties without having to loop through them all? because the object I have is huge and I need to get the min/max value every two seconds. (The values of the object keeps changing).

3 Answers
3

There's no way to find the maximum / minimum in the general case without looping through all the n elements (if you go from, 1 to n-1, how do you know whether the element n isn't larger (or smaller) than the current max/min)?

You mentioned that the values change every couple of seconds. If you know exactly which values change, you can start with your previous max/min values, and only compare with the new ones, but even in this case, if one of the values which were modified was your old max/min, you may need to loop through them again.

Another alternative - again, only if the number of values which change are small - would be to store the values in a structure such as a tree or a heap, and as the new values arrive you'd insert (or update) them appropriately. But whether you can do that is not clear based on your question.

This is great for IE7/8. Cheers @Niet the Dark Absol
–
ojhawkinsOct 27 '13 at 23:35

It's not necessarily true that min and max loop through the array to get their values. It's more feasible that they quicksort the array and select min and max values based on that result
–
goonerifyMar 16 at 14:16

1

@goonerify The fastest sort is O(n log n), which is inherently slower than the O(n) that just scanning through once would be...
–
Niet the Dark AbsolMar 16 at 15:11