Using %DynamicObject and %DynamicArray as general purpose data structures

Traditional Caché Objectscript has the multi-dimensional array as its main form of complex data structure and the $order command as the main means of traversing said data structures. But newer versions of Caché ObjectScript also have data structures that are direct parallels of what languages such as JavaScript provide, in the form of %DynamicObject and %DynamicArray. These have an easy to use iterator feature via the %GetIterator method, and even a handy built-in literal syntax for constructing new objects.

Given all these nice features, are there any disadvantages to using these fancy new features? I imagine performance might not be as fast and memory usage might be a bit higher compred to plain old arrays. And of course, this makes the code not backwards compatible to older versions of Caché which did not have these features. Are there any other caveats I should be aware of?


In my opinion schema-less data (NoSQL, dynamic objects, globals) does not particularly exist. All that happens when you create schema-less data structures is that data validation and enforcing schema becomes someone else problem. Usually that means application programmer. That's why if  possible, consider using strict schemas - the more assumptions about the data you can guarantee, the less validation client application need to do. Also process of data cleansing, reporting and so on become much easier.

That said there are some use cases where using schema-less data is the way to go:

  • For new applications/mock-up/PoC, when schema is unknown
  • When schema is very extensive and changes often
  • When speed is very important and data is retrieved by key (no complex queries)

To sum up, don't use schema if creating it and maintaining it would be a considerably more time-consuming affair than creating and maintaining application/reporting level data validation.

That said I see dynamic objects available in Caché mainly as a means to convert data from/to JSON.

With InterSystems IRIS we introduced DocDB - document database, based on dynamic objects, check it out.

Also mentioning @Stefan Wittmann.

Eduard - I think Arcady was asking from a purely data-processing perspective.  I.e. if you are already have a bunch of in-memory data and need to process/iterate on it, is it better to stick it in traditional structures and use $Order, or is it equivalent to stick it into the new structures for iterating and processing?  I think it's a good question and I would also like to hear what people think :)

I agree with Benjamin - the question is more about globals/locals vs DynamicObject/DynamicArray. 

@Arcady Goldmints-Orlov could you share the examples of code with globals/locals and Dynamic structures to illustrate two approaches more clear?