Module:table/size: Difference between revisions

From Linguifex
Jump to navigation Jump to search
m Protected "Module:table/size": Highly visible template/module ([Edit=Allow only template editors and administrators] (indefinite) [Move=Allow only template editors and administrators] (indefinite))
 
m 1 revision imported
 
(No difference)

Latest revision as of 11:22, 21 April 2026

Documentation for this module may be created at Module:table/size/doc

local next = next
local pairs = pairs

--[==[
This returns the size of a key/value pair table. If `raw` is set, then metamethods will be ignored, giving the true table size.

For arrays, it is faster to use `export.length`.]==]
return function(t, raw)
	local i, iter, state, init = 0
	if raw then
		iter, state, init = next, t, nil
	else
		iter, state, init = pairs(t)
	end
	for _ in iter, state, init do
		i = i + 1
	end
	return i
end