title: Slim Framework response incredibly slow when returning JSON
body: <p>I'm building a simple API based on Slim framework 3.8.1. I'm having an issue where the response is taking ~10 seconds to render. I've verified by process of elimination that it's the actual render of the response that's taking ~10 seconds--- it's not the database query, application bootstrap, etc.</p>
  
  <p>I'm loggin the data I'm getting from the endpoint and it retrieves and renders it more or less instantly. The route actually even renders the JSON output right away in the browser, but then continues to spin for ~10,000ms.</p>
  
  <p>Not sure what's going on here, especially since it's a pretty small set of data. Verified that this is happening in various browsers and also tests in REST clients like Postman.</p>
  
  <p>I verified the middleware has long since executed when this waiting has happened, so that doesn't appear to be it.</p>
  
  <p>Here's the minimal version of what I'm doing.</p>
  
  <pre><code>&lt;?php
  $api = new Slim();
  
      $api-&gt;any('/{plant}/{noun}', function ($request, $response, $args) {
  
          return $response-&gt;withStatus($status)-&gt;withJson(
                              $my_json
                          );
      }
  </code></pre>
  
  <p>If I use <code>-&gt;write()</code> and just send a short text string instead it's fine. If I pass JSON to <code>-&gt;write()</code> it hangs the same, for ~10 seconds.</p>
  
  <p>Oddly though if I do something simple like <code>return $response-&gt;withStatus(404)-&gt;withJson(['foo'=&gt;'bar']);</code> it returns a response instantly, with the caveat that the response body is truncated and just shows <code>{"</code> instead of the full <code>{"foo":"bar"}</code> response I'm expecting.</p>
  
  <p>Here's the JSON body I'm passing successfully but that renders for ~10 seconds:</p>
  
  <pre><code>{
    "data": {
      "id": "14",
      "user_id": "1",
      "name": "foozzz",
      "description": "1q234567u12349",
      "sku": "",
      "price": "123.00",
      "shipping": {
        "r1-1": "123.00",
        "r1-1+": "123.00",
        "r2-1": "123.00",
        "r2-1+": "123.00"
      },
      "flexible_price": "1",
      "digital_fulfillment": "1",
      "physical_fulfillment": "1",
      "physical_weight": "0",
      "physical_width": "0",
      "physical_height": "0",
      "physical_depth": "0",
      "available_units": "-1",
      "variable_pricing": "0",
      "fulfillment_asset": "9",
      "descriptive_asset": "64",
      "creation_date": "1499186300",
      "modification_date": "1499707715",
      "variants": {
        "attributes": [],
        "quantities": [
          {
            "id": "13",
            "key": "\"{\\\"123\\\":\\\"PURPLE\\\",\\\"2442\\\":\\\"djdoos\\\"}\"",
            "formatted_name": "",
            "value": "13"
          },
          {
            "id": "14",
            "key": "\"{\\\"123\\\":\\\"PURPLE\\\",\\\"2442\\\":\\\"dskmkdjjd\\\"}\"",
            "formatted_name": "",
            "value": "10"
          },
          {
            "id": "15",
            "key": "\"{\\\"123\\\":\\\"dappsajd\\\",\\\"2442\\\":\\\"djdoos\\\"}\"",
            "formatted_name": "",
            "value": "123"
          },
          {
            "id": "16",
            "key": "\"{\\\"123\\\":\\\"dappsajd\\\",\\\"2442\\\":\\\"dskmkdjjd\\\"}\"",
            "formatted_name": "",
            "value": "81"
          }
        ]
      }
    },
    "status": 200,
    "status_uid": "commerce_item_200"
  }
  </code></pre>
  
  <p>Originally I was thinking it was slow because of the size of the response, but this also takes ~10 seconds to finish loading (once again it renders instantly and then spins for the duration:</p>
  
  <pre><code>{
    "status": 404,
    "status_uid": "general_404",
    "status_message": "Route not found, or server error",
    "error_name": "There was an error while getting a response",
    "error_message": "The request failed."
  }
  </code></pre>
  
  <p>Running this on PHP 5.6.30 on Apache/2.2.15 with no other problems like this. Any thoughts of why this might be occurring?</p>
  
  <p>Just to recap: it's taking ~10 seconds to load, but verified it's not the database or middleware. If I take out the return $response the page loads right away.</p>
  
  <p>Any thoughts? Thanks!</p>