Your AOI asset wasn't shared, so I had to test this with a random area. By looking at your code, the biggest issue I see is the sorting. It's an expensive operation that you don't need.
Here's my stab at a script calculating the monthly averages. This still might fail for your actual AOI - no way for me to tell.
var aoiCollection = ee.FeatureCollection([
ee.Feature(ee.Geometry.Point([0, 10]).buffer(100000).bounds(), {ADM3_EN: 'Some region'})
])
var startDate = ee.Date('1981-01-01')
var endDate = ee.Date('2022-01-01')
var temperatureCollection = ee.ImageCollection("ECMWF/ERA5_LAND/HOURLY")
.filterDate(startDate, endDate)
.filterBounds(aoiCollection.geometry())
.select('temperature_2m')
var monthOffsets = endDate
.difference(startDate, 'months')
.subtract(1)
var dateCollection = ee.FeatureCollection(
ee.List.sequence(0, monthOffsets)
.map(function (monthOffset) {
var date = startDate
.advance(ee.Number(monthOffset), 'months')
return ee.Feature(null, {date: date})
})
)
var monthlyMinMax = dateCollection
.map(function (dateFeature) {
var date = ee.Date(dateFeature.get('date'))
var minMax = ee.Image(
temperatureCollection
.filterDate(date, date.advance(1, 'month'))
.reduce(ee.Reducer.minMax())
)
return minMax
.reduceRegions({
collection: aoiCollection,
reducer: ee.Reducer.mean()
.combine(ee.Reducer.count(), null, true)
.combine(ee.Reducer.min(), null, true)
.combine(ee.Reducer.max(), null, true)
.combine(ee.Reducer.stdDev(), null, true),
scale: 11132
})
.map(function (feature) {
return feature.set({
year: date.get('year'),
month: date.get('month'),
})
})
.filterMetadata('temperature_2m_max_count', "not_equals", 0)
})
.flatten()
print(monthlyMinMax)
Map.centerObject(aoiCollection)
Map.addLayer(aoiCollection)
https://code.earthengine.google.com/b2763c1b8d11add95295e187e7493558