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

256
Visualizações
Why is nulling a value faster than undefining it (javascript v8)

Heyho tested a bit around with maps vs objects in Javascript and found out that nulling is much faster (2,5 times) than putting the value to undefined or compared to simply delete the property

https://www.measurethat.net/Benchmarks/Show/15592/0/compare-nulling-undefining-and-deleting-of-javascript-o

If you are wondering why i allways create the map and or the javascript Object i did it so that every test has the same "overhead".

EDIT: also got this one which got a logical mistake in it(setting a value to from null to null or setting it from undefined to undefined) https://www.measurethat.net/Benchmarks/Show/15587/0/delete-vs-null-vs-undefined-vs-void-0-vs-objectcreatenu

and here the result is a bit more extreme 4mio ops (from null to null) vs 4k ops (undefined to undefined)

I know the test isnt really relevant its pure interest im asking :).

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

(V8 developer here.)

Microbenchmarks are misleading! Don't waste your time on them.

Setting an object property to null or to undefined has the same speed. Guaranteed.

Considering the significant difference that your test reproducibly shows, I got curious and dug in a bit. Turns out measurethat.net's framework code is... let's say... far from perfect: it uses "direct eval" in a way that introduces huge performance artifacts for accessing globals (that's one of several reasons why "never use direct eval!" is common advice), and one of JavaScript's historical accidents is that while null is a reserved keyword, undefined is just a global variable. See here for how ridiculous it gets:
https://www.measurethat.net/Benchmarks/Show/15627/0/accessing-null-vs-undefined
If you know what's going on, you can sidestep the issue:
https://www.measurethat.net/Benchmarks/Show/15635/0/null-vs-undefined-iiffe
But keep in mind that in a real app that doesn't use eval, you wouldn't see any of these differences; you're just playing games with a bad benchmark runner there!

Another thing that's really weird on that site is that when editing test cases and "validating" them, they produce very different results (I've seen 100x!) compared to "running" them after submitting them.

In short, I wouldn't trust any of the reported numbers on that site.

All that said, it makes sense that delete a.a is slower, because deleting an object property is a more complicated operation than overwriting an existing property's value. Importantly (and your benchmark doesn't show this, because it's too simple!), deleting properties often has non-local effects, i.e. it's not the deletion itself that's necessarily slow, but other parts of your app might get slowed down as side effects. We generally recommend not to use the delete keyword at all. Deleting entries in Maps is a different story: that's perfectly fine, as Maps are built to support that efficiently.

about 4 years ago · Juan Pablo Isaza Relatório

0

Javascript objects have to also account for which keys are "enumerable". Deleting a key removes it from the list of keys that enumerable. This is similar to removing an element from an array which is a slower operation than simply overriding a value with null.

I'm not sure why setting the value to undefined is slower. I would assume it's because of similar reasons as a missing key has a value of undefined.

about 4 years ago · Juan Pablo Isaza Relatório

0

I would say that it's because delete actually have to delete, compared to null that will just empty its content.

Comparison

  • Delete remove the property, making it slower but free some memory.
  • Nullifying is faster but the property, while null, the property still exists.

An object with 3000 property that are null takes more space in the ram than an empty object.

In conclusion

While performance and the technical aspects of JavaScript are very interesting, and performances in general should'nt be completely ignored, this is beyond unnoticeable and you should'nt care about it in real-life scenarios.

Note:

This is my personal understanding and should'nt be taken as official informations. Feel free to correct me in the comments.

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