=proto void duk_compact(duk_context *ctx, int obj_index); =summary
Resizes an object's internal memory allocation to minimize memory usage.
If the value at obj_index
is not an object, does nothing.
Although compaction is normally safe, it can fail due to an internal error
(such as out-of-memory error).
Compaction is not a final operation and has no impact on object semantics. Adding new properties to the object is still possible (assuming the object is extensible), but causes an object resize. Existing property values can be changed (assuming the properties are writable) without affecting the object's internal memory allocation. An object can be compacted multiple times: for instance, if a new property is added to a previously compacted object, the object can be compacted again to minimize memory footprint after the property addition.
Compacting an object saves a small amount of memory per object. It is generally useful when (1) memory footprint is very important, (2) an object is unlikely to gain new properties, and (3) when compaction can be applied to enough many objects to make a significant difference.
If Object.seal()
, Object.freeze()
, or
Object.preventExtensions()
is called, an object is automatically
compacted.