In Google Apps Script, execution time matters. With quotas on simultaneous runs and total runtime, long or inconsistent function execution can cause failures or delays. This is especially important when using services like LockService, where you need to define realistic wait times.
This article shows how to track execution time and errors in your functions—helping you stay within limits and optimize performance as your scripts grow.
Build a Simple Performance Tracker
Choose Where to Store Your Metrics
Apply Tracking to Your Existing Functions
The trackPerformance function takes three parameters:
func: the function you want to monitor
functionName: a string identifier for logging
args: an optional array of arguments to pass to func
It starts by recording the current time, then attempts to execute the given function with the provided arguments.Â
If the function runs successfully, it stores the result; if it fails, the error is caught and stored for later logging.
If an error was captured, it gets re-thrown so it can still be handled or reported as needed.Â
2. Choose Where to Store Your Metrics
To make performance tracking actionable, you'll want to log each execution’s data somewhere persistent. A simple and effective way is to store the metrics in a Google Sheet.
Here’s an example of how you can do that with the logMetrics function:
3. Apply Tracking to Your Existing Functions
Once you’ve set up your tracker and decided how to log the results, the final step is to wrap any function you want to monitor using trackPerformance.Â
This lets you measure execution time and automatically log any errors without changing the function's core logic.