Created
November 22, 2022 13:05
-
-
Save giovannamoeller/7a21ec0fc3097a42fcde295e152c1690 to your computer and use it in GitHub Desktop.
Big O - Example with JS
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
| function mesurePerformance(func, array) { | |
| const startTime = performance.now() | |
| func(array) | |
| const endTime = performance.now() | |
| console.log(`Tempo de execução: ${endTime - startTime} milisegundos`) | |
| } | |
| function func1(array) { | |
| return 1 + array[0] | |
| } | |
| function func2(array) { | |
| let sum = 0 | |
| for(let i = 0; i < array.length; i++) { | |
| sum += array[i] | |
| } | |
| return sum | |
| } | |
| function func3(array) { | |
| const pairs = [] | |
| for(let i = 0; i < array.length; i++) { | |
| for(let j = i; j < array.length; j++) { | |
| pairs.push([array[i], array[j]]) | |
| } | |
| } | |
| return pairs | |
| } | |
| function generateArray(numberOfElements) { | |
| const array = [] | |
| for(let i = 0; i < numberOfElements; i++) { | |
| array.push(i) | |
| } | |
| return array | |
| } | |
| const numberOfElements = 1000 | |
| const array = generateArray(numberOfElements) | |
| mesurePerformance(func1, array) | |
| mesurePerformance(func2, array) | |
| mesurePerformance(func3, array) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment