Javascript memory impact of null vs undefined

半城伤御伤魂 提交于 2020-08-17 06:39:48

问题


I work in an area where memory utilization is very important to us, as we don't run on your classic web browsers / hardware.

We use null quite a lot in our application, one thing that has not been clear to me is if null takes up more space than assigning a variable to undefined.

Do we know if one or the other is more costly on memory usage?

Thanks for help!


回答1:


As you can see in this jsperf test, null seems to be slightly faster in the chrome (V8, just like nodejs) which might indicate it being slightly more performant.

Since undefined is a longer string than null, the JIT compiler has to save 4 bytes more to memory when using undefined instead of null while parsing. Consider that memory aswell.

The garbage collection doesn't care about the value being either null or undefined. The difference should be minimal. You should consider the consequences regarding types of using null instead of undefined though. Enforcing the usage of either for the sake of performance might do more harm than good.



来源:https://stackoverflow.com/questions/50854916/javascript-memory-impact-of-null-vs-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!