Josh Pearce
Josh Pearce

Reputation: 3455

Why is ContactsApp.getContactById() slow

I'm getting a contact by id from within a google sheet script function and it takes between 10 - 35 seconds to return the contact. Here is the code:

var id = 'http://www.google.com/m8/feeds/contacts/xxxxxxxxx%40gmail.com/base/xxxxxxxxxx';
var contact = ContactsApp.getContactById(id);

Is there any way to speed that call up? Getting all 300+ contacts in a group takes less than a second:

var myContactsGroup = ContactsApp.getContactGroup('System Group: My Contacts');

Upvotes: 0

Views: 220

Answers (1)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17613

App Script does get slow when used with Spreadsheets (and other products). Just imagine all the rows and columns it has to traverse. What you can do is find ways to optimize your operations. Here's what I found:

App Script Best practices

  • Minimize calls to other services

  • Collaborate using Team Drives if possible

  • Use batch operations
  • Avoid libraries in UI-heavy scripts
  • Use the Cache service
  • Explanations are included in the docs link.

Don't make several calls when you can do it in one.

calls are relatively expensive, so making fewer calls is naturally going to improve performance. Secondly, if you can batch your calls (more on batching in a moment), it means that you're less likely to interleave reads and writes (gets and sets).

There's a code comparison included in the blog.

Upvotes: 1

Related Questions