Consider the following javascript data structure:
let sensors = {
sensor1: {
min: 1.00,
max: 9.00,
data: [
{
timestamp: 1517760374400,
value: 1.00
},
{
timestamp: 1517760374500,
value: 2.00
},
{
timestamp: 1517760374600,
value: 9.00
},
{
timestamp: 1517760374700,
value: 1.00
},
{
timestamp: 1517760374800,
value: 3.00
},
{
timestamp: 1517760374900,
value: 1.00
},
{
timestamp: 1517760375000,
value: 9.00
},
{
timestamp: 1517760375100,
value: 8.00
},
]
},
// sensor2, sensor3, etc...
}
Imagine there could be thousands of timestamped data for each sensor.
Initially you can easily set a min / max value, every time an object is added by checking if it is bigger or smaller than the current max
But the tricky part and my question is this:
What is the size of the array is limited - in this case we would set it to a length of 8.
Whenever a new item after item 8 is added (the limit is reached), the 1st item will be removed, and the nth item will be pushed into the end of the array.
The problem is that there can be more items with the same value, and even if there isn't we have no way of knowing which min / max is next without iterating the entire array once again
This is supposed to be scalable to thousands of array items, and is to be run roughly every second with ideally - as low cpu utilization as possible - I don't really think looping over thousands of items every second will be effecient enough.
Do you see another other ways of keeping track of min / max values of an array which is changing like this ever second?