Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
Compare 2 elements

Below is a typical way to check if the focus is on an element certain (txtCmd),

if(document.activeElement === txtCmd){
                return
}

I'm worried the operator=== would compare the entire element tree (every attribute and every sub-element) that would be low performance.

Does just compare the elements' Ids is better?

document.activeElement.id === txtCmd.id

PS: There are no different elements with identical ID in the page.

Edit:

I'm from C++. I want something like pointer comparison in C++,

   if(&a == &b) {}
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

=== doesn’t perform any deep comparisons, so it’s not low-performance.

An alternative is the .isSameNode() function:

b.isSameNode(a);

Edit:

I'm from C++. I want something like pointer comparison in C++,

if(&a == &b) {}

That essentially is what == and === do in JavaScript when 2 objects are tested for equality.

JavaScript’s quality operators compare objects by reference if the operands are both objects. a == b and a === b are true only if both a and b reference the same object.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you compare objects the === operator will perform a reference comparison, so it will only return true only if both operands points to the same object.

It will not cost you more performances but it just does not the same thing than comparing property litteral values.

For a deep comparison "by value" between objects, === doesn't work. You'd have to use a recursive entry by entry comparison, or compare stringified versions of the objects.

Example

const o1 = {"a":1};
const o2 = {"a":1};
const o3 = o1;
console.log(o1 === o2); // false
console.log(o1 === o3); // true
console.log(JSON.stringify(o1) === JSON.stringify(o2)) // true

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda