Very interesting GAS performance results – run locally using htmlservice

A little while ago, I published an item on GAS performance, showing that a complex calculation test would take about 100 times as long on GAS as the same thing in regular javascript running locally on a browser, and 10 times as long as the same thing on VBA.

So the question is – is it really running the same thing? To make sure, I showed how to retrieve and run GAS code directly in the browser, bypassing GAS altogether.

As a final comparison, I’ll also show to use HTML service to cajole the GAS code to be run locally in the browser but, this time directed from GAS.

The test code

function testCompareSpeed() {
function speedTest() {
  useTimer(‘x’,’10000 iterations of color comparison’).start();
  for (var i = 1 ; i <= 10000 ;i++) {
    compareColors (i, VBCOLORS.vbWhite – i);
  return useTimer().report();

GAS direct

Running directly in GAS – 26 seconds.
x : elapsed 26278 : iterations 1 : 10000 iterations of color comparison :

Locally on client, retrieving code from GAS

To see how to do this, take a look at retrieve and run GAS code directly in the browser. The test is here 
Running directly in GAS – 0.2 seconds.
x : elapsed 203 : iterations 1 : 10000 iterations of color comparison :

Locally on client, pushed by HTML service.

HTMLservice code will run clientside, as described here. So I reasoned that if I could publish the code, and cause it to be executed locally I should get results similar to the pure javaScript version – but I was wrong – 15 seconds is better than 26, but nowhere near 0.2.

Firstly, here’s the template you need to do this.

  // the source from GAS
   // do the test and report it
   alert (speedTest());

And the code

I use the same libraries as in  Publishing with Google Apps Script to play around with the GAS source, but here’s the specific code for populating the template above with the GAS scripts so that they can be run locally.

You can try it here, or look at it here.


Well I don’t really have a conclusion. Does anyone else have similar experiences? Here’s what I know
  • I am running exactly the same code in all 3 javascript versions, since the code is being served directly from GAS.
  • The VBA version is a simple translation
  • I notice that the cajoled GAS code has been caja-ized (if that’s a word) with oodles of code wrapped around it.
  • If I simplify the Maths, then GAS runs much more quickly – so I figured I’m probably introducing a memory leak that only manifests itself on GAS/Caja – yet the time taken is purely linear – 1000 iterations takes 10 times as long as a 100 – wouldn’t a memory leak be spiky or non-linear. How to debug that when it only appears if wrapped up in caja? 

For more stuff like this, see from VBA to Google Apps Script, and for more detail on this post see Serving up javaScript from GAS

(function() {
var po = document.createElement(‘script’); po.type = ‘text/javascript’; po.async = true;
po.src = ‘’;
var s = document.getElementsByTagName(‘script’)[0]; s.parentNode.insertBefore(po, s);

Author: brucemcp

Leave a Reply

Your email address will not be published. Required fields are marked *