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

308
Visualizações
hashmap on Buffer objects appears to be wrong, why

There are two different Buffer objects (different values) in a JavaScript, but in JavaScript object, they are treated as being equal. Any idea why? How to force them to be different keys for a JavaScript? Thanks

my node.js version is 10.15.1 on Ubuntu 18.04.

var buf1 = Buffer.from([
    10,
    33,
    45,
    77,
    10,
    33,
    46,
    166,
    140,
    190,
    5,
    241
  ]);
var buf2 = new Buffer.from([
    10,
    33,
    45,
    77,
    10,
    33,
    46,
    168,
    215,
    216,
    5,
    241
]);
let a = {};
a[buf2] = "jon";
console.log(a[buf1]);  //output  jon
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

a[buf2] is equivalent to a[buf2.toString()], which is equivalent to a[buf2.toString('utf-8')], which is the cause of your problem: your buffers don't hold valid UTF-8 data, so when converting to an UTF-8 string, invalid code points are replaced by the Unicode replacement character, U+FFFD (encoded as 0xEF 0xBF 0xBD in UTF-8).

To show this, you can run the following code:

console.log('buf1', Buffer.from(buf1.toString()).toString('hex'));
console.log('buf2', Buffer.from(buf2.toString()).toString('hex'));

The result is the hexadecimal representation of each buffer as it's used as a property "name" (replacement character highlighted):

buf1 0a212d4d0a212eefbfbdefbfbdefbfbd05efbfbd
                   ^^^^^^^^^^^^^^^^^^  ^^^^^^
buf2 0a212d4d0a212eefbfbdefbfbdefbfbd05efbfbd

As you can see, they are equivalent.

about 4 years ago · Juan Pablo Isaza Relatório

0

a[buf2] = "jon"; is exactly the same as a['[object: Object]'] = "jon";, because key of object must be a string, if it's not string it will be converted to string using toString method.

What you wanted to do is:

var buf1 = Buffer.from([
    10,
    33,
    45,
    77,
    10,
    33,
    46,
    166,
    140,
    190,
    5,
    241
  ]);
var buf2 = new Buffer.from([
    10,
    33,
    45,
    77,
    10,
    33,
    46,
    168,
    215,
    216,
    5,
    241
]);
let a = {};
a['buf2'] = "jon";
console.log(a[buf1]);  //outputs undefined
console.log(a['buf2']); // outputs "jon"
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