Benchmark: Process Costs for Checking Value in Array using Google Apps Script

Gists

Kanshi Tanaike

Introduction

There is a maximum executing time for Google Apps Script (GAS). That is 6 minutes. And, in the case of the custom function and the simple trigger, it is 30 seconds. Ref So users always have to pay attention to reducing the process cost of the scripts. Especially, it is very important to know the process costs for the array processing, because array processing is often used for spreadsheets and Google APIs. I have already reported about the array processing at “Benchmark: Loop for Array Processing using Google Apps Script with V8” and “Search for Array Processing using Google Apps Script”. In this report, the process cost checking a value in a one-dimensional array using Google Apps Script has been investigated.

Experimental procedure

In the experiment, a 1-dimensional array including a specific value in the center of the array was used. For example, if the array length is 11, the search value is put into the 6th element. The specific value is searched from the array using Google Apps Script under enabling V8 runtime. When the value is found, true is returned. This process was carried out by 13 methods, and the process costs (the processing time) were measured by changing the array length. You can see the sample scripts for these methods in “Appendix” section.

By the way, at GAS, the processing time is not stable as you know. So the average value for more than 200 times measurements was used for each data point which is shown by the figures. At this time, the fluctuation of the average values was less than 1 %. I worry that each detailed-data point in my environment might be different from that in another user’s environment. But I think that the trend of this result can be used.

Results and discussions

Benchmark: Process Costs for Checking Value in Array using Google Apps Script

Fig. 1: Process times with increasing the number of array lengths.


Benchmark: Process Costs for Checking Value in Array using Google Apps Script

Fig. 2: Process times with increasing the number of array lengths. This figure is the enlarged view of Fig. 1.


Figure 1 shows the process times with increasing the number of array lengths. Figure 2 shows the enlarged view of Fig. 1.

From Figs. 1 and 2, the following results were obtained.

Summary

In this report, the process cost for checking a value in a 1-dimensional array using Google Apps Script was investigated. As the result, the following results were obtained.

As a note, I have to describe that this is the result for Google Apps Script. For other languages, this result might be different. And also, the process cost of this report might be modified by a future update of Google.

Appendix

The following scripts were used for measuring the process cost.

const functions = [
  { name: "indexOf1", f: (array => array.indexOf(search) != -1) },
  { name: "indexOf2", f: (array => Boolean(~array.indexOf(search))) },
  { name: "lastIndexOf1", f: (array => array.lastIndexOf(search) != -1) },
  { name: "lastIndexOf2", f: (array => Boolean(~array.lastIndexOf(search))) },
  { name: "includes", f: (array => array.includes(search)) },
  { name: "Set", f: (array => new Set(array).has(search)) },
  { name: "find", f: (array => Boolean(array.find(e => e == search))) },
  { name: "findIndex", f: (array => Boolean(array.findIndex(e => e == search))) },
  { name: "some_test", f: (array => array.some(e => new RegExp(search).test(e))) },
  { name: "some_match", f: (array => array.some(e => e.match(new RegExp(search)))) },
  { name: "some", f: (array => array.some(e => e == search)) },
  { name: "filter", f: (array => Boolean(array.filter(e => e == search).length)) },
  {
    name: "for_loop", f: (array => {
      let res = false;
      for (let i = 0; i < array.length; i++) {
        if (array[i] == search) {
          res = true;
          break;
        }
      }
      return res;
    })
  },
];

 Share!