Google Apps javascript timer

If you are working with Google Apps Script, one of your priorities is definitely going to be a simple timer. Everything takes longer than you are used to, and you have to be very careful what you ask for.

Here’s a simple timer for Google Apps script (actually javaScript generally).

You use it like this

// cache read: steps through all cells in a sheet
   useTimer('a').start('read cached inputdata sheet values using foreach');
   cache.forEach( function(v,r,c) {
       x = v;
     } 
   );
   useTimer('a').stop();
   
  // cache write: copies the values to somewhere else on the same sheet
   useTimer('b').start('make a copy on same sheet');
   for (var i = 0; i < nr ; i++)
   for (var j = 0; j < nc; j++) {
     cache.setValue(cache.getValue(i+1,j+1),i+1,j+1+nc);
   }
   cache.commit();
   useTimer('b').stop();
   
  // cache write: copies the font Colors to somewhere else on the same sheet
   useTimer('c').start('make a copy on same sheet of font Colors');
   for (var i = 0; i < nr ; i++)
   for (var j = 0; j < nc; j++) {
     cacheFontColors.setValue(cacheFontColors.getValue(i+1,j+1),i+1,j+1+nc);
   }
   cacheFontColors.commit();
   useTimer('c').stop();
 ///.......etc...etc...
  // report all timers
    DebugPrint(useTimer().report());

and you get this 

~t~ : elapsed 2609 : iterations 1 : Master Timer : 
a : elapsed 534 : iterations 1 : read cached inputdata sheet values using foreach : 
b : elapsed 286 : iterations 1 : make a copy on same sheet : 
c : elapsed 977 : iterations 1 : make a copy on same sheet of font Colors : 
d : elapsed 378 : iterations 1 : make a copy on another sheet : 
e : elapsed 429 : iterations 1 : make a copy on another sheet of font Colors : 

For more infomation and to get the code for this and other stuff see, Google Apps Script Timer collection.

About brucemcp 225 Articles
I am a Google Developer Expert and decided to investigate Google Apps Script in my spare time. The more I investigated the more content I created so this site is extremely rich. Now, in 2019, a lot of things have disappeared or don’t work anymore due to Google having retired some stuff. I am however leaving things as is and where I came across some deprecated stuff, I have indicated it. I decided to write a book about it and to also create videos to teach developers who want to learn Google Apps Script. If you find the material contained in this site useful, you can support me by buying my books and or videos.