I'm struggling to understand how to format and use objects and keep things efficient.
I come from an embedded C background.
In my node red home automation implementation I have a global array of objects, one per 'thing'. The things may be anything from a lightswitch to a heating zone to a weather object. There are currently around 100 'things' and eventually it will have maybe 200-300.
I work on a thing from the array by using
let myThing = global.get("thingsStatus["+ thingId + "]").
and then individual properties eg.
I have a common object format for most simple things and it has maybe 20 variables. Some things - such as heating or weather are much more complex and may add a further 10-15 variables.
I have done this by creating an optional 'extraobject' for the complex things. The extraobject takes a different form according to the type - weather/heating etc.
let windSpeed = myThing.extraobject.windSpeed
This all works but is prone to error when developing when I forget the property is part of the extra.I think I have made a rod for my back!
Q1.Would it be any less efficient to put all the properties for every thing type as the first level in the ting object?
This would mean that every item in the array had possibly dozens of properties that are meaningless.
Eg a lightswitch would have a windspeed property.
Q2. Is there a better way of having an array of objects where there are several different structures for the different object types?
Each object may have 20 'common' properties, and then whatever additional properties it's particular type of thing requires.
So to answer you questions - it wouldn't be any less efficient for each object to have all of its properties at the top level. It would not mean that every item in the array has dozens of meaningless properties. They only have the properties you chose to set on them.
Thanks – that helpful. I’ll take a backup and make the change and see what happens.
Behind the scenes – can you explain how the Java engine know how to pull a specific variable from object when object has a variable of the same name, alongside several other variables that don’t exist in object?
This bit of logic is behind my flawed thinking that all objects in an array probably need to be the same structure for efficiency. (It comes from my C background and pointers logic understanding.)
So the object "stored" in the array is its own entity with its own structure and properties. In fact, the array just holds a reference (pointer) to the "real" object (somewhere in memory).
object has a member property named
object has a property named
.fred is besides the point, the array elements point to individual objects (so they can have the same or different properties - it doesn't even have to be an object - it could be an integer or string or ...)
Thanks. I think I get it. (I'm perhaps being too concerned with what happens under the hood and trying to second guess what's happening!)
In fact, I try to keep my larger objects to a schema that I can also replicate pretty well in MQTT topics. That way, if (usually when) I want to have the data in memory (the object) AND want to use the event handling that comes with MQTT, I can do so using the same (or at least very similar) data structures.
This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.