空对未定义的Javascript内存影响 [英] Javascript memory impact of null vs undefined

查看:48
本文介绍了空对未定义的Javascript内存影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个内存使用对我们非常重要的领域工作,因为我们无法在您的经典Web浏览器/硬件上运行.

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

我们在应用程序中大量使用了null,我尚不清楚的一件事是,如果null占用的空间大于将变量分配给undefined所占用的空间.

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?

感谢帮助!

推荐答案

您可以在此jsperf中看到测试,Chrome浏览器中的null似乎更快(V8,就像nodejs一样),这可能表明它的性能更高.

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.

由于undefined是一个比null长的字符串,因此JIT编译器在使用undefined而不是null进行解析时,必须将4个字节以上的内存保存到内存中.还要考虑该内存.

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.

垃圾回收无关紧要值可以为null或未定义.差异应该很小.您应该考虑使用类型使用null而不是未定义的后果.为了性能而强制使用这两种方法可能弊大于利.

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.

这篇关于空对未定义的Javascript内存影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆