Search code examples
angularjsnode.jscordovapodio

Multiples Calls to REST API or One Call with large response body with Javascript


In order to reduce response time or to shorten time that the user waits for data when rendering views, i'm trying to determine what's best in interacting with a REST API. I'll be getting an array of items with 5-7 fields, e.g. name, title, imgUrl. I can either make one big call and traverse through the response to get the data that I need or make 5-7 requests to get the exact information that I need.

There are two issues with making the large call.

  1. ALOT of data is returned with each item. I tested with retrieving 3 items and it took about 899 ms.
  2. The fields that I need cannot simply be referenced by a key. Each Item is returned as array of fields. Each field is an object and I can only determine which fields I need by traversing each object and reading its field_id. Its returned like this:

    item :[ { ... field_id: 3423423, ... }, { ... field_id: 343434, ... } ... ]

I can rather send one request with an item_id and field_id and I'll get the field I need, but I'll have to make 7 of these calls. Which is better?


Solution

  • I recently had to make a similar decision and I ended up adding methods to the back-end that packaged the data up in a format that was more suitable for consumption. If you are able to do that, I would recommend that approach.

    I suspect that the API is out of your control.In that case, I would probably go with multiple async calls so that you can provide feedback while the data is retrieved. Using async calls and promises you can let all the individual pieces get retrieved in the background in whatever order they come in and then assemble them from there.