Google App Script: identify performance/execution speed bottleneck part of the code

Viewed 144

Google App Script: identify performance/execution speed bottleneck part of the code. Anyone already succeeded in this in general in terms of techniques? Can you share your examples?

Below is my small script to output HTML webpage with simple table from Google ClassRoom. I'm noob in those technologies as of right now, but trying to make progress in learning.

Maximum 100-150 table rows to output by this script. Could not understand why script runs more than 5-8 minutes to complete its work & eventually output those 100-150 rows HTML table.

Maybe I'm using array wrong way or anything else? Or maybe it is the nature of Google App Script to run slowly overall?

function doGet() {
  var res = listCoursesHTML();
  var html = HtmlService.createHtmlOutput();
  html.append("<table border = \"1\">");
  html.append("<tr><th>Имя преподавателя</th><th>Почта</th><th>Название курса</th><th>Название ОП (Учебная группа)</th></tr>");
  for(result in res)
  {
    html.append("<tr><td>"+res[result].Name +"</td><td>"+res[result].email +"</td><td>"+res[result].courseName +"</td><td>" +res[result].courseDescription+"</td></tr>");
  }
  html.append("</table>");
  //return HtmlService.createHtmlOutput(out[0]);
  return html;
}

function listCoursesHTML() {
  var courses = [];
  var ownerId = [];
  var pageToken = null;
  var optionalArgs = {
    pageToken: pageToken,
    pageSize: 9999
  };
  var results = [];

  while (true) {
    var response = Classroom.Courses.list(optionalArgs);
    // @ts-ignore
    var courses = response.courses;
    if (!pageToken) {
       break;
    }
  }
  if (courses.length === 0) {
    Logger.log("No courses found.");
  } else {
    Logger.log("Courses:");
    // @ts-ignore
    for (course in courses) {
      // @ts-ignore
      results.push({
        "Name": getName(courses[course].ownerId),
        "email": getEmail(courses[course].ownerId),
        "courseName": courses[course].name,
        "courseDescription": courses[course].description
        });
    }
  }
  return results;
}

function getEmail(id) {
  return Classroom.UserProfiles.get(id).emailAddress;
}

function getName(id) {
  return Classroom.UserProfiles.get(id).name.fullName;

}


1 Answers

Apps Script provides a good guide on best practises in order to keep your code clean and specially in order to keep it efficient.

  • One that might be relevant to your case scenario is using Batch operations rather than setting elements one by one.

  • Also, please be very careful when using a potential infinite loop (while(True)) and try replacing it instead for an if if(pageToken) or a while depending on a variable while(pageToken).

  • As @Cooper mentioned for in is not recommended to be used to iterate over arrays as it was rather designed to iterate over properties of an object and therefore its performance might get decreased when using it with arrays. As described in this literature it is better to use a simple for loop or a forEach loop for arrays.

  • Finally I recommend you reading further and making performance test between append, innerHtml and html() as each of these might be better in terms of performance depending on the case scenario.

Related