· Oct 5, 2017

Using dynamic objects and dynamic arrays

Hi everyone,

I really like using dynamic objects and dynamic arrays, Usually ending up with an SQL query that returns a JSON object i throw into my application via REST and work out things from there.

Recently I noticed that I keep using dynamic objects as simple storage solutions like:

set settings.customerSelection = 1

instead of ending up with several variables flying around.

This certainly is way more inefficient than having simple variables but does it make a huge difference? Especially considering how much more organized the code appears?


Discussion (2)0
Log in or sign up to continue

Interesting thought Sebastian.

We haven't run dedicated benchmarks on this topic, but I can provide some guidelines.  You have to acknowledge that creating a dynamic object or a dynamic array is the most expensive part of your code style compared to just using plain variables. You are instantiating an object.

If you run a simple benchmark like the following you can observe a difference:

ClassMethod TestAll()
    set iterations = 1000000
    set start = $P($H,",",2)
    for i=1:1:iterations {
        do ..TestDynamicObject()
    set time = $P($H,",",2) - start
    w "Dynamic Object test case took "_time_" seconds",!
    set start = $P($H,",",2)
    for i=1:1:iterations {
        do ..TestVariables()
    set time = $P($H,",",2) - start
    w "Variable test case took "_time_" seconds",!

ClassMethod TestDynamicObject()
    set do = {}
    set do.setting1 = "value"
    set do.setting2 = "value"
    set do.setting3 = "value"
    set do.setting4 = "value"
    set do.setting5 = "value"
    set do.setting6 = "value"
    set do.setting7 = "value"
    set do.setting8 = "value"
    set do.setting9 = "value"
    set do.setting10 = "value"

ClassMethod TestVariables()
    set setting1 = "value"
    set setting2 = "value"
    set setting3 = "value"
    set setting4 = "value"
    set setting5 = "value"
    set setting6 = "value"
    set setting7 = "value"
    set setting8 = "value"
    set setting9 = "value"
    set setting10 = "value"

The result on my local machine looks like this:

USER>do ##class(User.DOAPerf).TestAll()
Dynamic Object test case took 13 seconds
Variable test case took 0 seconds

So there is a significant difference IF your code is called often enough to actually make the overhead noticeable. In the above example I called each method 1 million times. If your code is not called that often you will not observe a difference.

As a general advice I would not nest the dynamic objects or dynamic arrays when used as you describe it, as this increases the number of object creations.

Long story short: Stick with variables in performance-critical parts of your application. In other parts of your application you can use dynamic objects or dynamic arrays as temporary objects for sure, just make sure you only nest them when absolutely required. Passing complex structures between method calls with dynamic objects can be very elegant.