**How to enhance your Lambda function performance with memory configuration?**

--

I have a lambda function that’s taking a few seconds to execute, and that’s not related to a cold start. It’s the code in the handler function that’s taking time, and that’s because it’s CPU and memory intensive.

Performance problems related to cold start can be enhanced through provisioned concurrency and managing the “Init” code. You can read about this in my previous articles:

- How to speed up your Lambda function
- Effortlessly Juggling Multiple Concurrent Requests in AWS Lambda

We know the performance is related to CPU and Memory, so the solution might be as simple as increasing the memory. Isn’t it?

Let’s do a small demonstration comparing the execution of the Lambda with different memory configurations. It’s worth noting that we cannot explicitly control the Lambda CPU; it’s automatically increased as we increase the memory. From the AWS docs:

The amount of memory also determines the amount of virtual CPU available to a function. Adding more memory proportionally increases the amount of CPU, increasing the overall computational power available. If a function is CPU-, network- or memory-bound, then changing the memory setting can dramatically improve its performance.

**Testing the performance of a Lambda function**

For this demo let’s use the following Lambda function that I generated with the help of ChatGPT:

`export const handler = async (event) => {`

const matrixSize = 100; // Size of the square matrix

const matrix = generateMatrix(matrixSize);

const result = performMatrixMultiplication(matrix);

return result;

};

function generateMatrix(size) {

const matrix = [];

for (let i = 0; i < size; i++) {

const row = [];

for (let j = 0; j < size; j++) {

row.push(Math.random());

}

matrix.push(row);

}

return matrix;

}

function performMatrixMultiplication(matrix) {

const size = matrix.length;

const result = new Array(size);

for (let i = 0; i < size; i++) {

result[i] = new Array(size);

for (let j = 0; j < size; j++) {

let sum = 0;

for (let k = 0; k < size; k++) {

sum += matrix[i][k] * matrix[k][j];

}

result[i][j] = sum;

}

}

return result;

}

The Lambda function generates a large square matrix, performs matrix multiplication, and…