Koa: Performance issue while returning a large Object

Created on 21 Nov 2017  路  5Comments  路  Source: koajs/koa

I have a 1.7MB object to send.

Doing this will cause a TTFB time of 330ms (that's ok) and a Content download time of 4s in Chrome.

router.get('/heavy', async function heavyRoute(ctx, next) {
  const result = await collectHeavyData()
  ctx.body = result // my 1.7MB object
  console.log('less than 100ms and we are here')
})

The same logic written with .NET cause a TTFB time of 2s (that's ok, my bad) and a Content download time of 92ms.

The response are the same, the Koa route code execute in less than 100ms, but the client get the complete response really slowly.

Am I missing something?

question

Most helpful comment

try using streams or a more optimized JSON.stringify() module

All 5 comments

The issue appear even on production mode.

SW stack

  • Node.js 8.9.1
  • Typescript 2.6.1
  • koa 2.4.1
  • koa-router 7.3.0

try using streams or a more optimized JSON.stringify() module

Just as @jonathanong wrote, @greguz - use Streams since it is where Node.js shines and has the biggest advantage over other back-end technologies. Stream will basically divide your object/file into small chunks of data and send them one by one. It may sounds crazy but results are astonishing.

I am going to close this issue. Feel free to re-open if you still believe this to be an issue with Koa.

Was this page helpful?
0 / 5 - 0 ratings