title: Greasemonkey Javascript, Speed up xmlHttpRequest
body: <p>I want to know the methods to speed up an XHR (xmlHhttpRequest) GET.</p>
  
  <p>I have an application that keeps sending an GM_xmlHttpRequest every X seconds.<br>
  The essential idea is, the faster I get the a response that satisfy a condition, faster I can take an action.<br>
  What I've done so far, is to set a timeout, like 2000ms, and track the number of timeouts and success.<br>
  Depending on these numbers I increase or decrease the X value.<br>
  Example:<br></p>
  
  <blockquote>
    <p>sent 10 requests, timeout_count=8, success_count=2.<br>
    if (timeout_count==8) xSeconds=xSeconds+100;<br>
    if (timeout_success==10) xSeconds=xSeconds-10;</p>
  </blockquote>
  
  <p>Something like this.
  Of course, this isn't a performance, and it's only a way I found to improve the speed of the calls depending on the internet speed factor.</p>
  
  <p>What I need to know is... </p>
  
  <p>Is there another methods to increase the speed of the requests of a "keep-sending" XHR application?</p>
  
  <p>Methods like setting headers with specific values, or maybe some API, or maybe some programming approach? Something/Someway that make the requests lighter...</p>
  
  <p>Thank you very much.</p>
  
  <p>EDIT: As the user Bergi demanded, I'm adding this explanation:
  somesite.com is a feed site, like... if I run a regExp to find a certain string in the response, I will send-me an e-mail. Something like this.</p>
  
  <p>My code is something like this:</p>
  
  <pre><code>var count_success = 0;
  var count_timeout = 0;
  var requestTime = 1000;
  var httpRequest = function() {
  
      setTimeout(function() {
          httpRequest();
          httpRequest_get();
      },requestTime);
  }
  
  var httpRequest_get = function() {
      GM_xmlhttpRequest({
          method: 'GET',
          url: "http://www.somesite.com/",
          headers: {
              "User-Agent": "Mozilla/5.0",
              "Accept": "text/xml",
          },
          timeout: 2000,
          ontimeout: function() {
              timeout_count++;
              count_timeout++;
              if(count_timeout==5) {
                  requestTime=requestTime+100;
                  count_timeout=0;
              }
          },
          onload: function(response) {
              // do something with the response
              count_success++;
              if(count_success==5) {
                  requestTime=requestTime-50;
                  count_success=0;
              }
          }
      });
  }
  </code></pre>
  
  <p><strong>Answer to the comment made by the user Bergi:</strong></p>
  
  <p>It's not under my control. It's a feed site, as I mentioned. I want to monitor the offers showed by this feed, each offer has a price, and when an item under a certain price is matched, I send-me an e-mail. Then seeing this e-mail poping out in my phone, I can decide either to buy it or not. </p>
  
  <p>There is doc to their API, as far as it goes, I only have the code I can study under his page. I started with the making requests for the feed site, just www.somesite.com, but the requests were tooking too much, the length of the string was greater than 80k. Then, I monitored their feed to see what their site are doing to update the feed, then I found the XHR they do every X time, I used to replicate this request, then the string went down to 10k or less sometimes. </p>
  
  <p>Well, you got the idea, The faster I do it, the better. Matter of fact, I want to do it under 300ms. I already achieved the avg of 800ms. Also, you must be questioning yourself, why so fast? what's the matter between 800ms and 300ms? The answer is, there is also another ppl scripting the same thing and their scripts are faster than mine. I'm looking for somehow reduce the size of the request, maybe changing the headers, but I'm not getting much success on it. The internet factor is also important, I know this for sure, but I can't afford it at the moment. I just hope there are ways of optimizing it without depending only of the connection speed factor. Thanks.</p>