Qantas 94 Heavy
Qantas 94 Heavy

Reputation: 16020

Why does initialising an object's property with null increase the speed?

For some reason, it appears (in Chrome at least) that if you create an object and initialise each of the properties with a null value and then assign a value to each, it is faster than simply leaving the object blank initially. Why is this the case (test)?

Upvotes: 3

Views: 197

Answers (1)

Nick H
Nick H

Reputation: 11535

V8 (Chrome's Javascript engine) has optimizations that define hidden classes to represent particular objects in your Javascript. It is better able to do this when you declare the object's properties at creation time.

There are two advantages to using hidden classes: property access does not require a dictionary lookup, and they enable V8 to use the classic class-based optimization, inline caching.

https://developers.google.com/v8/design#prop_access

Upvotes: 2

Related Questions