Hey, friends today I will teach you How to Improve Performance of Google Apps Script with Memoization. so let get started with today Code snippets. Getting different problems is altogether gives a very different experience. today the Code snippets I am going to share with you is How to Improve Performance of Google Apps Script with Memoization.
A Google Drive folder contains a bunch of CSV files and that is very much required to write a Google Script to find a particular value in the CSV files. The solution is simple:
- Use the Drive API to get a list of CSV files in the specified folder.
- Parse the CSV files one by one using the
Utilities.parseCsv()function. - Read the CSV file, line by line, until the value is found and return the line number.
const findContentInCSVFiles = (folderId, searchString) => {
const folder = DriveApp.getFolderById(folderId);
const files = folder.getFilesByType("text/csv");
while (files.hasNext()) {
const file = files.next();
const fileContent = file.getBlob().getDataAsString();
const linesOfData = Utilities.parseCsv(fileContent, ",");
let found = false;
let lineNumber = 0;
for (; lineNumber < linesOfData.length && !found; lineNumber += 1) {
const line = linesOfData[lineNumber];
found = line.find((element) => element === searchString);
}
if (found) {
return `${searchString} found in line #${
lineNumber + 1
} of file ${file.getName()}`;
}
}
return "String not found :(";
};
You might also like our trending code snippets
- How to Make all Shapes the Same Size in Google Slides
- Best way to Import Lodash in your JavaScript Projects for Lowest Bundle Size
- न्यूनतम बंडल आकार के लिए अपने जावास्क्रिप्ट प्रोजेक्ट्स में लोडाश आयात करने का सबसे अच्छा तरीका
How To Optimize Google Script Performance
The code for reading CSV files and finding the required value is straightforward but inefficient. You must perform the same costly operation for each value to be searched in the CSV file folder.
Memoization is a simple optimization technique for improving the performance of your Google Apps Script code. The basic idea is to use closures to cache the results of an expensive function call. When the function is called again with the same arguments, the cached result is returned rather than calling and executing the function again.
const memoize = (func) => {
// Cache for storing the previously computed results
const cache = {};
return (...args) => {
// Serializer to convert N arguments to a string
const key = JSON.stringify(args);
if (typeof cache[key] === "undefined") {
cache[key] = func(...args);
}
return cache[key];
};
};
const memoizedFindFunction = memoize(findContentInCSVFiles);
const findContentInFiles = () => {
const FOLDER_ID = "<<folder id>>";
const SEARCH_STRING = "hello world!";
const response = memoizedFindFunction(FOLDER_ID, SEARCH_STRING);
Logger.log(resonse);
};
The memoization function is called with the arguments of the original function. The result of the function is stored in a cache and returned when the same arguments are passed again.

