When I run the following code in Cloud Functions, it takes more than 2 seconds.
When I run it locally, it takes about 600 milliseconds.
What are the possible causes?
import * as functions from 'firebase-functions'
import axios from 'axios'
const headers = { 'accept': 'application/json', 'x-access-key': '...', 'x-access-secret': '...' }
exports.functionName = functions.https.onRequest(async (req, res) => {
try {
console.log('request 1 start')
const response1 = await axios.get(`https://api.sample.com/users/${req.body.userId}`, { headers })
console.log('request 1 completed')
const response2 = await axios.post(`https://api.sample.com/contents1/${response1.data.id}`, {}, { headers })
console.log('request 2 completed')
const response3 = await axios.post(`https://api.sample.com/contents2/${response2.data.id}`, {}, { headers })
console.log('request 3 completed')
res.send(response3)
} catch (error) {
res.send(error)
}
})
Metrics
In Cloud, each asynchronous request (axios.get/post) is taking up to almost 1 second.

Hypothesis
- It is inevitable that Cloud Functions with Outbound networking will take a long time
- Cold start is not the cause (as the execution time does not decrease after the second execution).
What I tried
I think I tried all the methods described in the official Firebase documentation.
- Minimum number of instances: I set it to "2" in the GCP console, but no improvement
- Increase Memory allocated: I increased it to 1GB, but no improvement
- Use global variables to reuse objects: in the above code,
headerobject is the one - HTTP Keep-Alive: I wrote the following code, but no improvement
const httpAgent = new http.Agent({ keepAlive: true })
await axios.get(`https://api.sample.com/users/${req.body.userId}`, { headers, httpAgent })