James Mulholland

/

Notes

Go home

How to measure the performance of express API endpoints

Last updated: 2020-05-24

The following middleware works well as a lightweight method for logging the performance of Express API endpoints to the console.

Use it when you want to quickly monitor and improve your backend's performance.

const logPerformance = (req, res, next) => {
  const startInMs = performance.now()

  res.on('finish', function () {
    const endInMs = performance.now()
    const elapsed = endInMs - startInMs
    console.log('url: ', req.url)
    console.log('time: ', elapsed + 'ms')
  })

  next()
}

// ...
app.all('*', logPerformance) // disable for production```