Using Objects to Increase Performance
When you call APIs that return objects, it is always better to store the object in a local variable than to make multiple calls to the API. It is also better to call a single API to get everything you need, rather than calling into a more basic API to get one Thing at a time. Depending on your server configuration, APIs could require round trips off of the server to other resources.
Example: Get property aspect information
In this example, we retrieve aspect information for each property of a Thing. To do that, we get the Thing information as JSON and then pull the information from the property definition. Therefore, we are making two API calls to build the JSON information.
Things[thingName] searches for the required Thing by name.
GetMetadataAsJSON() walks the Thing entity hierarchy to build the JSON information for the Thing.
Wrong way:
In this example, we search for the Thing each time and build the JSON representation on every property call.
var propertyNames = ["p1", "p2"]
for (var i = 0; i < propertyNames.length; i++)
logger.warn("Property [" + propertyNames[i] + "] cacheTime: " + Things[me.name].GetMetadataAsJSON().propertyDefinitions[propertyNames[i]].aspects.cacheTime)
}
Right way:
This example will search for the Thing once, build the JSON object once, and then get values from property definitions for each property. If we were doing multiple operations on the Thing, we could build a Thing variable from Things[thingName]. In this case, we only need the property definitions, so that is what we save.
var propertyNames = ["p1", "p2"]
var propertyDefinitions = Things[me.name].GetMetadataAsJSON().propertyDefinitions
for (var i = 0; i < propertyNames.length; i++)
logger.warn("Property [" + propertyNames[i] + "] cacheTime: " + propertyDefinitions[propertyNames[i]].aspects.cacheTime)
}
Example: Get property names
In this example, we build a list of property names by getting the property definitions from a Thing and building a list of names. We make two API calls.
Things[thingName] searches for the required Thing by name.
GetPropertyDefinitions() calls into the Thing and walks the hierarchy to build the list of property definitions.
Wrong way:
This is a bad example that calls the APIs to get the number of properties and then calls into the definitions to get the name and description each time. The Thing search and property definition are called at least twice for every property.
for (var i = 0; i < Things["MyThing"].GetPropertyDefinitions().length; i++) {
var newEntry = new Object();
newEntry.name = Things["MyThing"].GetPropertyDefinitions().rows[i].name;
newEntry.description = Things["MyThing"].GetPropertyDefinitions().rows[i].description;
result.AddRow(newEntry);
}
Right way:
This example calls the Thing and property definition API only once for all properties.
var propDefs = Things["MyThing"].GetPropertyDefinitions();
for (var i = 0; i < propDefs.length; i++) {
var newEntry = new Object();
newEntry.name = propDefs.rows[i].name;
newEntry.description = propDefs.rows[i].description;
result.AddRow(newEntry);
}
Was this helpful?