ANS-0521 · SUITESCRIPT DEVELOPMENT
How to Manage and Optimize NetSuite Script Execution Limits?
Understand the three primary factors—time, governance, and execution count—that can limit script performance and learn strategies to prevent script termination.
Short answer
NetSuite scripts are subject to time, governance, and execution count limits. To prevent script termination, developers should implement rescheduling logic for scheduled scripts using N/task and N/runtime modules, optimize code to reduce execution time and API calls, and simplify complex functions to avoid exceeding JavaScript's call stack size. Monitoring memory usage is also crucial.
Scenario
NetSuite scripts, including scheduled scripts, client scripts, and user event scripts, can encounter unexpected termination due to various system limitations. Developers often face challenges when scripts exceed allocated processing time, consume too many governance units, or exhaust the JavaScript virtual machine's execution count. Understanding these limitations is crucial for designing robust and efficient NetSuite automations.
Solution
NetSuite scripts are subject to three primary execution limits: time, governance, and execution count. Addressing these proactively is essential for stable script operation.NetSuite imposes a time limit on script execution. Scheduled scripts typically run for up to approximately one hour before termination. The nlapiRequestURL API call also has a timeout limit: "Note a timeout occurs if the initial connection takes > 5 seconds and/or the request takes > 45 to respond." Moreover, most browsers have implemented a timeout for HTTP requests, the standard is 5 minutes. So if a script takes too much time loading, it could trigger the browser's request timeout.For scheduled scripts, developers must implement rescheduling logic using the N/task and N/runtime modules to manage execution time effectively. For any other script types, it is recommended to review the script's design to reduce execution time or delegate the logic to a scheduled script. Avoiding long loops and calling external services can significantly improve performance.NetSuite's API Governance limits scripts from having too many heavy API calls and slowing down the system and the user. Going over the metering limit of a script type will kill the execution of the script. For more information on the subject, please refer to the NetSuite Help Center.For scheduled scripts, developers should implement rescheduling logic using the N/task and N/runtime modules to manage governance unit consumption and allow the script to halt its execution and restart where it left off with its metering reset. For any other script types, it is recommended to review the script's design to reduce the number of API calls or delegate the logic to a scheduled script.This is more a JavaScript limitation than an actual NetSuite limitation. Basically, every script is executed in a virtual machine in the browser or server side instead of being compiled like other languages. This means that there is a limit to the amount of data the virtual machine can hold and process. Every function call, variable declaration, if statement and loop can count toward the scripts' "execution count". If the execution count of a script is exceeded, the script will crash. A "Maximum call stack size exceeded" error can be an example of exceeded execution counts. The best way to exceed this count is by doing a recursive function call. Calling functions within a function, even more so if there are loops, will become very heavy on the system and eventually it will crash.There is no magic workaround for this; developers need to review their design and try to find a way to reduce the number of calls that their script does. For example, developers can get rid of variables that are not needed:
var x = i + j;var text = "The number is " + x;You can easily put the "i+j" in your string instead of defining a new variable only for this.If script complexity remains a challenge, consulting with a technical lead or senior developer is recommended.NetSuite also monitors the memory usage of scheduled scripts and has a limit of 50 Megabytes per script. Exceeding this limit would result in a SS_EXCESSIVE_MEMORY_FOOTPRINT error. For more information, consult the NetSuite Help Center.
Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
