Table.clear: Difference between revisions
From GiderosMobile
Created page with "__NOTOC__ '''Available since:''' Gideros 2022.3<br/> '''Class:''' table<br/> === Description === Sets the value for all keys within the given table to nil. This causes th..." |
No edit summary |
||
| (2 intermediate revisions by one other user not shown) | |||
| Line 5: | Line 5: | ||
=== Description === | === Description === | ||
Sets the value for all keys within the given table to nil. This causes the # operator to return 0 for the given table. The allocated capacity of the table's array portion is maintained, which allows for efficient re-use of the space. | Sets the value for all keys within the given table to nil. This causes the # operator to return 0 for the given table. The allocated capacity of the table's array portion is maintained, which allows for efficient re-use of the space. | ||
< | <syntaxhighlight lang="lua"> | ||
table.clear(t) | table.clear(t) | ||
</ | </syntaxhighlight> | ||
'''This function does not delete/destroy the table provided to it. This function is meant to be used specifically for tables that are to be re-used''' | '''This function does not delete/destroy the table provided to it. This function is meant to be used specifically for tables that are to be re-used''' | ||
''table.clear(t)'' is faster and better for performance in loops or frequently reused tables because it avoids creating garbage collection pressure and preserves internal array/hash capacity. | |||
''table = {}'' allocates a new table object every time it runs, which increases garbage collection workload if used repeatedly in performance-critical code. | |||
=== Parameters === | === Parameters === | ||
| Line 15: | Line 19: | ||
=== Example === | === Example === | ||
< | <syntaxhighlight lang="lua"> | ||
local grades = {95, 82, 71, 92, 100, 60} | local grades = {95, 82, 71, 92, 100, 60} | ||
print(grades[4], #grades) --> 92, 6 | print(grades[4], #grades) --> 92, 6 | ||
| Line 23: | Line 27: | ||
-- no potentially expensive array resizing will occur | -- no potentially expensive array resizing will occur | ||
-- because the capacity was maintained by table.clear. | -- because the capacity was maintained by table.clear. | ||
</ | </syntaxhighlight> | ||
{{Table}} | {{Table}} | ||
Latest revision as of 15:57, 30 September 2026
Available since: Gideros 2022.3
Class: table
Description
Sets the value for all keys within the given table to nil. This causes the # operator to return 0 for the given table. The allocated capacity of the table's array portion is maintained, which allows for efficient re-use of the space.
table.clear(t)
This function does not delete/destroy the table provided to it. This function is meant to be used specifically for tables that are to be re-used
table.clear(t) is faster and better for performance in loops or frequently reused tables because it avoids creating garbage collection pressure and preserves internal array/hash capacity.
table = {} allocates a new table object every time it runs, which increases garbage collection workload if used repeatedly in performance-critical code.
Parameters
t: (table) table to clear
Example
local grades = {95, 82, 71, 92, 100, 60}
print(grades[4], #grades) --> 92, 6
table.clear(grades)
print(grades[4], #grades) --> nil, 0
-- If grades is filled again with the same number of entries,
-- no potentially expensive array resizing will occur
-- because the capacity was maintained by table.clear.