react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Fetch response.json() seems slower than response.text()

Open mchev2 opened this issue 10 years ago • 5 comments

Just wanted to document this performance issue (incase it really exists).

fetch_from_network = function() {
  fetch(url, {
      headers: {
        'Cache-Control': 'no-cache'
      }
    })
    .then(function(res){
      // If we do res.json, for unknown reason it is slowing the render of our very complex view. res.text() does not seem to have this issue
      return res.text();
    })
    .then(function(json){
      // setstate is called in this.process
      this.process( JSON.parse(json) );
    }.bind(this));
}.bind(this);

mchev2 avatar Mar 11 '16 18:03 mchev2

@mchev2 Can you do some profiling and post the results?

satya164 avatar Mar 11 '16 19:03 satya164

I did some independent tests and did not see any difference between res.text and res.json. I had to add in caching so res.text will work for my use case.

I'm not sure where the issue could be. Perhaps the json is too heavily nested and so there is a bottleneck. I will close this for now and let you know if I find anything else.

Thank you

mchev2 avatar Mar 14 '16 20:03 mchev2

@mchev2 just bringing this guy's issue to your attention, could be similar.

https://stackoverflow.com/questions/36499603/taking-too-much-time-to-call-promises-then-function/36501330#36501330

MrMotski avatar Apr 08 '16 13:04 MrMotski

I have encountered the same issue, the responseText comes back straight away in milliseconds, but when it convert to json using .json(), it takes a few seconds, the interesting thing is if I click the screen during the .json() parsing period, it get the json data back straight away

rickli1989 avatar Apr 24 '16 13:04 rickli1989

Make sure you are:

  • profiling on device, not with Chrome debugger
  • profiling in production
  • measuring just the response.json() time and not anything else like setState.

ide avatar Apr 24 '16 16:04 ide