Lodash _.size() method
What you’ll learn
- How
_.size(collection)counts elements for arrays, strings, objects, and common builtins. - Why
null/undefinedsafely yield0instead of exceptions. - When native
.lengthorObject.keysis clearer than a helper. - Try each example in the editor (
?tryit=1,2,3) with Lodash from a CDN.
Prerequisites
Skim _.includes() when you need membership—not cardinality—or revisit the collection hub.
- You distinguish arrays from plain objects and know strings expose
length. - You can open Try-it labs or run snippets locally.
Overview
_.size is the polymorphic yardstick—counts playlist tracks, configuration keys, characters in prompts, or Map entries without branching on type first.
Lists & strings
Arrays and array-like values defer to length; strings report UTF-16 code units.
Keyed records
Plain objects tally enumerable own string keys—similar footprint to _.keys.
Null-safe defaults
Missing collections collapse to zero—handy for optional analytics payloads.
Syntax
_.size(collection) - collection: value Lodash classifies as a collection—arrays, strings, plain objects, Maps, Sets, etc.
- Returns: non-negative integer length/count.
- Mutates: never—read-only introspection.
Size of an array
Mirrors array.length while staying consistent with other Lodash collection APIs.
import size from "lodash/size";
size([10, 20, 30]);
// → 3 Count keys on a plain object
Non-array collections expose counts via enumerable own keys—ideal before iterating unknown config blobs.
import size from "lodash/size";
size({ env: "prod", region: "us-east", tier: "gold" });
// → 3 Measure a string
Follows string length semantics—count UTF-16 code units, same as native indexing caveats for emoji.
import size from "lodash/size";
size("hello");
// → 5 📋 _.size vs .length, Object.keys, Reflect
| API | Outcome | Best when |
|---|---|---|
_.size(collection) | Unified count | Mixed collection types arrive through one code path |
collection.length | Arrays/strings only | You already know the shape is sequential |
Object.keys(obj).length | Own enumerable keys | No Lodash dependency on plain objects |
Reflect.ownKeys variants | Symbols + strings | You need spec-accurate key listings beyond lodash defaults |
Pitfalls to avoid
Arrays with holes
size trusts length—holes still inflate numeric spans exactly like native arrays.
Hidden versus visible keys
Non-enumerable or symbol keys may be skipped—match expectations with Object.getOwnPropertyDescriptors when auditing.
Grapheme clusters
Complex emoji sequences count as multiple UTF-16 units—pair with segmenters when UX cares about visual glyphs.
❓ FAQ
Summary
- Purpose:
_.size(collection)returns how many elements or enumerable keys a collection exposes. - Contrast: drop down to native accessors when types are fixed and dependencies should shrink.
- Next: Lodash _.some(), Lodash _.shuffle() (previous), or collection hub.
_.size(null) and _.size(undefined) both resolve to 0—Lodash treats absent collections as empty instead of throwing like some native accessors.
6 people found this page helpful
