Skip to content

Instantly share code, notes, and snippets.

@giovannamoeller
Created November 22, 2022 13:05
Show Gist options
  • Select an option

  • Save giovannamoeller/7a21ec0fc3097a42fcde295e152c1690 to your computer and use it in GitHub Desktop.

Select an option

Save giovannamoeller/7a21ec0fc3097a42fcde295e152c1690 to your computer and use it in GitHub Desktop.
Big O - Example with JS
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