Optimizing Script Performance

Objective

After completing this lesson, you will be able to optimize the script to improve story performance.

Optimize Script Performance

In this lesson, we explore story scripts in order to better understand a script's impact on story performance and three scenarios where the story scripts can be optimized to improve scripting performance.

We'll start by looking at a story that isn't performing well and requires a number of interactions between the Main and SandboxWorker threads, slowing down the story performance. Then, we show you how to optimize scripts that contain:

  1. Multiple use of the getValue and setValue methods.
  2. Multiple use of the getResultSet method.
  3. Unfiltered use of the getResultSet method.

Summary

In the video, we explore three common issues with script performance and best practices for optimizing the script.

  1. Multiple use of the getValue and setValue methods.

    In our scenario, the text box was misused to store intermediate states, like the typical use case of a variable, while the script was still being processed.

    At the beginning of the script, you can declare an empty text variable and, within the loop, concatenate the required results into the text variable. As the last step of the script, the text is applied to the text box.

    With that, you only need the one interaction between the two threads, which is to apply the final text in the text box.

  2. Multiple use of the getResultSet method.

    In our scenario, the getResultSet method is called in the header as well as four times within the loop.

    Instead of asking the Main thread each and every time to hand over the result set, it is best to only ask for it once and store it in a local variable. This local variable can be used as many times as needed without any additional interaction between the SandboxWorker and the Main thread.

  3. Unfiltered use of the getResultSet method.

    In our scenario, the script uses the getResultSet API without specifying any filter like offset, limit or selection. Without specifying a filter, the returned array of this API contains an element for each cell of the result set.

    Use the getResultSet optional Selections argument that can be used to restrict the return array.

Scripting in the onResultsetChanged Event

There is one final important script optimization topic that story developers must keep in mind when scripting in the onResultsetChanged event.

Whatever you code in the onResultsetChanged event script, make sure you do not change the same result set after you do. This triggers another onResultsetChanged event, and an endless loop is created. The only way a user can cancel the loop is to close the browser window.

Log in to track your progress & complete quizzes