V
V
Vladimir Zuev2020-06-23 07:26:16
Google Apps Script
Vladimir Zuev, 2020-06-23 07:26:16

When should you create functions?

Hello.
I noticed that when creating functions for better reading of the code of the main program, the execution time noticeably increases when the function is called and entered.
At first, I tried to reduce the number of lines of code by creating functions that could reduce the number of lines of code. For better code readability. But I realized that this leads to an increase in the execution time of the program.
So the question is when should functions be created? Did I correctly draw conclusions regarding the increase in program execution time due to functions? Please help.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Ivanov, 2020-06-23
@vladd56

An example of a noticeable number of functions https://github.com/contributorpw/lodashgs/blob/mas... The script running time grows minimally. Many use. It's not about the size. Moreover, the scripts are well compressed before execution. And this file https://github.com/contributorpw/alasqlgs/blob/mas... (do not open on a slow network) works fine even as a user-defined function, and there, for a second, only half a minute of execution time.
You gave an example SpreadsheetApp.flush(). This method stops all calculations and recalculates everything you have in the Table. It's probably why you're having problems.
Based on your previous questions, you enhance the work of the interface, put pressure on the impression. Well, in the browser it will work as it will, and it will not work to make it all work faster.
Advice. Give up

Everything stands out at once. Now you can see what range I'm working with. It is much clearer what falls within the range values.

, because it's all nonsense, and no one needs to look at the screen to understand what works and what doesn't. The user needs to know three things:
  1. Haven't counted yet
  2. Still thinks
  3. Already counted

The rest is darkness and fog for him, and you are wasting time on them.

M
Maxim Stoyanov, 2020-06-23
@stomaks

Well, without code, it makes no sense to talk about any kind of optimization.
The general principles are, try to access documents and the disk less often if you do.
If you are working with a spreadsheet, try to redraw the interface less frequently (spreadsheet.flush()).
Again, you need your code to understand what you are doing.
---
Maxim Stoyanov (stomaks), developer of Google Apps Script .
g-apps-script.com
stomaks.me

H
hint000, 2020-06-23
@hint000

execution time noticeably increases when a function is called and entered into it

The fact that time is growing is natural. But it will grow noticeably in the case when the function is called inside the loop and the loop is executed hundreds..thousands of times. Single calls should not give any noticeable slowdown.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question