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

Identify what makes your TTFB high so you can fix it

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

Read more

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay