admin管理员组

文章数量:1401264

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!

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!

Share Improve this question asked Jun 14, 2018 at 10:07 mrkgreggmrkgregg 1231 silver badge4 bronze badges 2
  • Possible duplicate of Does null occupy memory in javascript? – user8214858 Commented Jun 14, 2018 at 10:14
  • Check these answer: What reason is there to use null instead of undefined in JavaScript? – Yosvel Quintero Commented Jun 14, 2018 at 10:17
Add a ment  | 

1 Answer 1

Reset to default 6

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 piler 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.

本文标签: Javascript memory impact of null vs undefinedStack Overflow