Log Function Performance in Google Apps Script 📊
July 11, 2025
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.Â
/**
* Logs execution metrics for a given function.
* @param {Function} func - The function to track.
* @param {string} functionName - The name of the function being tracked.
* @param {Array} args - Arguments to pass to the function.
* @returns {any} - The result of the executed function.
*/
function trackPerformance(func, functionName, args = []) {
const startTime = new Date();
let result;
let errorMessage = '';
try {
result = func.apply(null, args);
} catch (error) {
errorMessage = error.toString();
}
const endTime = new Date();
const executionTime = (endTime - startTime) / 1000; // Convert to seconds
logMetrics(functionName, startTime, executionTime, errorMessage);
if (errorMessage) {
throw new Error(errorMessage); // Re-throw the error after logging
}
return result;
}
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:
/**
* Logs metrics to a Google Sheet for performance tracking.
* @param {string} functionName - The name of the function executed.
* @param {Date} startTime - The start time of the execution.
* @param {number} executionTime - Time taken for execution in seconds.
* @param {string} errorMessage - Error message if any occurred.
*/
function logMetrics(functionName, startTime, executionTime, errorMessage) {
const sheetName = 'Performance_Log';
const ss = SpreadsheetApp.getActiveSpreadsheet();
let sheet = ss.getSheetByName(sheetName);
if (!sheet) {
sheet = ss.insertSheet(sheetName);
sheet.appendRow(['Timestamp', 'Function Name', 'Execution Time (s)', 'Error']);
}
sheet.appendRow([startTime, functionName, executionTime, errorMessage || 'None']);
}
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.
/**
* Entry point for the web app. This will track execution metrics.
* @param {Object} e - The event parameter containing request information.
* @returns {HtmlOutput} - The HTML output for the web app.
*/
function doGet(e) {
return trackPerformance(handleGetRequest, 'doGet', [e]);
}