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 image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

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