question

joe.lavoine@gmail.com avatar image
joe.lavoine@gmail.com asked

General cloud execution time reduction tips?

Hello,

I was running into the 5 second cloud execution time limit earlier today (due to doing something inefficient). I was wondering if you had any general tips in the future for reducing times in general.

  • Do server API calls add meaningful time?
  • Should we aim for a lower # of function calls?
  • Does using log.info() add meaningful time?
  • Does using JSON.parse or stringify add meaningful time?
  • Any other general tips for reducing time?

And one other general question: Can we expect cloud call execution times to be steady for the same call/data? i.e. if I have a cloud call ManipulateData() that manipulates the same amount of data each time, will its execution time be about the same always?

Thanks,

Joe

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Joshua Strunk avatar image
Joshua Strunk answered

Do server API calls add meaningful time?

Yes, however it is small and as long as your under the api call limit this is likely not a worry.


Should we aim for a lower # of function calls?

No, The # function calls is likely to not have a major impact on performance. In addition, I promise code written while trying to reduce the number of function calls will have a major impact on your teams performance.


Does using log.info() add meaningful time?

Does using JSON.parse or stringify add meaningful time?

Any other general tips for reducing time?

Almost anything can add meaningful time to your execution if done in a loop. I use logs, parse, stringify liberally throughout my CloudScript and those have never been a source of headache.

The only non-algorithm source for poor CloudScript performance my team has encountered has come from array.push. Even a tiny number ( < 100) of them in your code will bring your CloudScript to a crawl. If you must push elements onto an array it is much faster to declare a c-style array of much greater capacity and manage pushing on it manually by just incrementing a counter when you push onto it.

3 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

brendan avatar image brendan commented ·

Very interesting info on array.push - we hadn't noticed that the V8 engine performed poorly with that!

One other thing I'd recommend checking is any use of http calls to external services. If those services are slow to respond, that could have a significant impact on your total processing time.

1 Like 1 ·
Joshua Strunk avatar image Joshua Strunk brendan commented ·

There is a good chance something else might have been going on as well(maybe some misuse of array.push on our part?). Though, as soon as we switched out raw array.push for our own preallocated array push implementation we went from 5+ down to 2-3 sec average.

0 Likes 0 ·
brendan avatar image brendan Joshua Strunk commented ·

It occurred to me that this is likely due to re-sizing of the arrays - a common issue back in the early stl days - so I had a quick look around and found a good write-up that confirms this: https://gamealchemist.wordpress.com/2013/05/01/lets-get-those-javascript-arrays-to-work-fast/.

So, pre-allocation of your arrays would likely be the simplest way to approach this, in Cloud Script.

1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.