10 JavaScript best practices for SCADA backend

Our youngest programmer MH just shared his thought about writing JavaScript for SCADA backend. He’s pretty observant and the ideas are straightforward. That said, JavaScript is so much fun to use in SCADA as compare to Basic-like language found in many traditional SCADA. Visual Basic is marked for death, and JavaScript is now and future. It’s a language that all youngsters who grow up with touch screen will be mastered. If you have been using traditional SCADA and haven’t used JavaScript for SCADA, please proceed to grab a free download immediately and enjoy the beauty of programming! You can do so much more with shorter code. Otherwise, if you already started using JavaScript for Ecava IGX SCADA, let’s learn from a youngster:

1. Understand getTag() and setTag() overhead
The two JavaScript functions, getTag and setTag are provided by IntegraXor for backend scripts. getTag is used to read a tag value and setTag is used to assign a value to a tag. Each of these function calls incurs significant processing time. The use of these functions in the backend scripts is inevitable. Users should minimize these function calls in each script to speed up the script execution time. One place to be cautious of is calling any of these functions in a loop.

2. Set the script triggering time wisely
Depending on what each script does, the script execution time may vary from a few milliseconds to a few seconds. If the execution time is longer than the triggering time interval, the script will not be executed on each interval exactly and each execution interval will be longer. It is advisable to set the interval to be one second or longer, unless you have high performance CPU or short scripts.

3. Know the differences between JavaScript and JScript
JScript is the Microsoft’s implementation of JavaScript. In IntegraXor, JavaScript is executed by the Google V8 JavaScript Engine, while JScript is executed by the Microsoft Active Script Engine. In the script configuration in the Project Editor, ActiveX is enabled by default. This means the script is treated as JScript and is executed by the Active Scripting Engine. If ActiveX functions are needed, ActiveX should be enabled. If the Active Scripting Engine is used, each script will be converted to a single function and all variables in the script will be local to that function.

4. Avoid Sleep function if V8 JavaScript Engine is used
If a script is intended to be executed by the V8 JavaScript Engine (ActiveX disabled), Sleep should not be called in the script. The V8 JavaScript Engine executes only one script at a time and Sleep will delay the execution of other scripts. IntegraXor will execute a script as JScript if the keyword “Sleep” is found in the script. Instead of relying on the Sleep function, use a timer to trigger the script.

5. Limit the number of scripts in a project
It is not advisable to have too many JScript scripts in a project. Too many JScript scripts will affect the performance of IntegraXor because each JScript script execution requires CPU resources. Ensure that each JavaScript script that runs on V8 has short execution time (less than 500 ms if possible) because the V8 JavaScript engine executes the scripts sequentially if two or more scripts are triggered at the same time.

6. Avoid unnecessary scripts if possible
I/O tags (e.g. from sensors or PLC devices) can be used to trigger alarms directly. If possible, avoid using I/O tags to trigger scripts to change the values of virtual tags, and then using the virtual tags to trigger alarms. Consider using the tag output expression to scale the tag value and use the scaled value to trigger an alarm. This will reduce the number of virtual tags and scripts.

7. Prefer local variables to global variables
In JavaScript, local variables are variables that are declared with the use of the keyword “var” in a function. Any variable declared outside of a function is a global variable. Two variables with the same name that are declared outside of any function in two different script files are treated as the same variable by the V8 JavaScript Engine. If the variable is modified when a script is executed, another script will access the modified value when it is executed at a later time. Using local variables will eliminate this issue.

8. Avoid infinite loops at all costs
Make sure your loops contain proper terminating conditions. Without a terminating condition, a loop will repeat forever. This is also known as an infinite loop. If it happens, the script will not finish executing. This may hold IntegraXor Server to proceed to next task while waiting for scripting execution to end.

9. Keep functions short and simple
There is no rule on the maximum number of lines for a function. A general rule of thumb is that a function should do only one thing.This practice may be hard to comply because you may end up writing many short functions, but in long term the benefits will outweigh the trouble to write many short functions. Short functions are easier to understand and maintain. They also encourage code reuse.

10. Understand JavaScript and observe the best practice
JavaScript is flexible but at the same time this advantage could be abused. There are many JavaScript *basic* best practices to observe on top of this list.

Share with us if you have your own tips as well!