JavaScript · Senior · Question 26 of 75

Why can adding properties to objects in different orders slow code down in V8?

Explanation

The engine optimizes property access by the object's "shape" (hidden class / shape). If objects of one type are created with different orders or sets of fields, the call site sees many shapes and loses the fast path. Initialize all fields in the constructor right away and don't remove them with delete.

Further reading

Problem with a question

JavaScript · Senior · question 26

Test yourself

Take the JavaScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz