You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

49 lines
1.7 KiB

=proto
void duk_compact(duk_context *ctx, int obj_index);
=summary
<p>
Resizes an object's internal memory allocation to minimize memory usage.
If the value at <code>obj_index</code> 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).
</p>
<p>
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.
</p>
<p>
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.
</p>
<p>
If <code>Object.seal()</code>, <code>Object.freeze()</code>, or
<code>Object.preventExtensions()</code> is called, an object is automatically
compacted.
</p>
=example
duk_push_object(ctx); /* [ ... obj ] */
duk_push_int(ctx, 42); /* [ ... obj 42 ] */
duk_put_prop_string(ctx, -2, "meaningOfLife"); /* [ ... obj ] */
duk_compact(ctx, -1); /* [ ... obj ] */
=tags
object
property
memory
=fixme
Clarify that compaction only affects an object's "own properties".