size()
size<
T>(collection):number
size<
T>(collection):number
Gets the size of collection by returning its length for array-like values or the number of own enumerable string keyed properties for objects.
DEPRECATED
Use array.length or Object.keys().length directly instead.
Type Parameters
T: T
The type of elements in the array, or the object type.
Parameters
Overload 1:
collection: T[]
The collection to inspect.
Overload 2:
collection: T
The collection to inspect.
Returns: number
The collection size.
See Also
Since
2.0.0
Also known as
length (Ramda, Effect) · size (Lodash, es-toolkit) · ❌ (Remeda, Radashi, Modern Dash, Antfu)
Example
const numbers = [1, 2, 3, 4, 5];
// ❌ Deprecated approach
const arraySize = size(numbers);
console.log(arraySize); // 5
// ✅ Recommended approach (for arrays)
const arraySizeNative = numbers.length;
console.log(arraySizeNative); // 5
// ✅ Recommended approach (for objects)
const obj = { a: 1, b: 2, c: 3 };
const objSizeNative = Object.keys(obj).length;
console.log(objSizeNative); // 3
How it works?
Gets the length of collection.
Deprecated: Use .length or .size directly.
Native Equivalent
// ❌ size(arr)
// ✅ arr.length
// ❌ size(map)
// ✅ map.size
Use Cases
Get collection length 📌
Get the number of elements in a collection.
const items = [1, 2, 3, 4, 5];
items.length;
// => 5
Count object properties
Get the number of properties in an object.
const config = { host: "localhost", port: 3000, debug: true };
Object.keys(config).length;
// => 3
Check if empty
Determine if a collection is empty.
const results = [];
results.length === 0;
// => true