title: ExpressJS Middleware bodyParser has very poor performance
body: <p>Few days ago we added NewRelic, APM to our Rest API, which is written in NodeJS and uses EXPRESS JS as a development framework.</p>
  
  <p>Now we see a lot of users experience poor response times, because of JSON parser middleware.</p>
  
  <p><strong>Here is one of those requests reported in NewRelic:</strong>
  <a href="https://i.stack.imgur.com/dfQjD.png" rel="noreferrer"><img src="https://i.stack.imgur.com/dfQjD.png" alt="enter image description here"></a></p>
  
  <p><strong>Drilled-down report:</strong>
  <a href="https://i.stack.imgur.com/dhSyG.png" rel="noreferrer"><img src="https://i.stack.imgur.com/dhSyG.png" alt="enter image description here"></a></p>
  
  <p>As you can see the most of the time is consumed by middleware JSON Parser.</p>
  
  <p>We were thinking maybe issue comes from big JSON payloads, which is sometimes sent out from API. But for this response, illustrated above, API returned data with contentLength=598, which shouldn't be too huge JSON.</p>
  
  <p><a href="https://i.stack.imgur.com/m53Kb.png" rel="noreferrer"><img src="https://i.stack.imgur.com/m53Kb.png" alt="enter image description here"></a></p>
  
  <p>We also use compression middleware as visible on drilled down request execution screenshot. Which should be reducing size of IO sent back and forth to clients.</p>
  
  <p>At this moment we have a doubt for a parameter limit which is passed to middleware when initialized. {limit:50kb} But when testing locally it doesn't make any difference.</p>
  
  <p><a href="https://i.stack.imgur.com/sImfM.png" rel="noreferrer"><img src="https://i.stack.imgur.com/sImfM.png" alt="enter image description here"></a></p>
  
  <p>We were thinking to switch with protobuf, also think about the way to parse JSON payloads asynchronously. Because JSON.parse which is used by middleware is synchronous process and stops non-blocking IO. </p>
  
  <p>But before staring those changes and experiments, please if anyone had same kind of problem to suggest any possible solution.</p>
  
  <p><strong>Benchmarking:</strong></p>
  
  <p>For benchmarking on local/stage environments we use JMeter and generate loads to check when such timeouts may happen, but we are not able to catch this when testing with JMeter.</p>
  
  <p>Thank You.</p>