Created
October 26, 2023 15:46
-
-
Save SamWSoftware/786182f92acf18c4789a91c8cd571c89 to your computer and use it in GitHub Desktop.
lambda cpu tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const handler = async (event) => { | |
// TODO implement | |
//#Source https://bit.ly/2neWfJ2 | |
const hz = (fn, iterations = 100) => { | |
const before = performance.now(); | |
for (let i = 0; i < iterations; i++) fn(); | |
return (1000 * iterations) / (performance.now() - before); | |
}; | |
// 10,000 element array | |
const numbers = Array(10000) | |
.fill() | |
.map((_, i) => i); | |
// Test functions with the same goal: sum up the elements in the array | |
const sumReduce = () => numbers.reduce((acc, n) => acc + n, 0); | |
const sumForLoop = () => { | |
let sum = 0; | |
for (let i = 0; i < numbers.length; i++) sum += numbers[i]; | |
return sum; | |
}; | |
// sumForLoop` is nearly 10 times faster | |
const hzReduce = Math.round(hz(sumReduce)); | |
const hzForLoop = Math.round(hz(sumForLoop)); | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify({ | |
memory: process.env.AWS_LAMBDA_FUNCTION_MEMORY_SIZE, | |
hzReduce, hzForLoop}), | |
}; | |
return response; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment