Reputation: 383
In my Lambda function (Node.js), I measured the time from the start of my function to the end of my function which is about 1400 milliseconds. However, the billed duration lambda gives me is 2800 milliseconds.
Is this normal? What could be the reason for this high discrepancy (it's basically double)?
Here's the code I used to measure
exports.handler = function(event, context, callback) {
let time = new Date();
... some logic runs ...
console.log(`{new Date().getTime() - time.getTime()}`);
callback(null, response);
}
Upvotes: 4
Views: 843
Reputation: 383
All credit to Mark B as in comments.
Solution is to add the line:
context.callbackWaitsForEmptyEventLoop = false;
The reason is AWS Lambda by default, for Node.js deployments, will wait for the event loop to be empty before returning. Thus, if you can set the above line to prevent this from happening.
Upvotes: 2