DEV Community

Cover image for Big O | Como calcular, na prática, em Javascript?
Luiz Calaça
Luiz Calaça

Posted on • Edited on

6 2

Big O | Como calcular, na prática, em Javascript?

Olá, devs! Aqui será uma descomplicação completa em relação ao cálculo de complexidade de algoritmos com o Big O. Iremos analisar esta função no vídeo:

function anotherBigOChallenge(input) {
  let a = 5; //O(1)
  let b = 10; //O(1)
  let c = 50; //O(1)
  for (let i = 0; i < input; i++) { //O(n) 
    let x = i + 1; //O(1)
    let y = i + 2; //O(1)
    let z = i + 3; //O(1)
   }

  for (let j = 0; j < input; j++) { //O(2n)
    let p = j * 2; //O(1)
    let q = j * 2; //O(1)
  }
  let whoAmI = "I don't know"; //O(1)
}
Enter fullscreen mode Exit fullscreen mode

Sugiro que, ao menos, saiba basicamente os conceito Big O no contexto constante O(1), liner O(n) e quadrática O(n^2).

E, agora, acrescentaremos um novo for e faremos um novo cálculo:

Em geral, podemos dizer que a complexidade do algoritmo é apenas linear, quadrática ou constante, sem detalhes como O(5n), pois a constante 5 não altera a linearidade.

Contatos
Email: luizcalaca@gmail.com
Instagram: https://www.instagram.com/luizcalaca
Linkedin: https://www.linkedin.com/in/luizcalaca/
Twitter: https://twitter.com/luizcalaca

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay