Lodash _.size() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.size(collection) counts elements for arrays, strings, objects, and common builtins.
  • Why null/undefined safely yield 0 instead of exceptions.
  • When native .length or Object.keys is 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

javascript
_.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.
1

Size of an array

Mirrors array.length while staying consistent with other Lodash collection APIs.

javascript
import size from "lodash/size";

size([10, 20, 30]);
// → 3
Try it Yourself
2

Count keys on a plain object

Non-array collections expose counts via enumerable own keys—ideal before iterating unknown config blobs.

javascript
import size from "lodash/size";

size({ env: "prod", region: "us-east", tier: "gold" });
// → 3
Try it Yourself
3

Measure a string

Follows string length semantics—count UTF-16 code units, same as native indexing caveats for emoji.

javascript
import size from "lodash/size";

size("hello");
// → 5
Try it Yourself

📋 _.size vs .length, Object.keys, Reflect

APIOutcomeBest when
_.size(collection)Unified countMixed collection types arrive through one code path
collection.lengthArrays/strings onlyYou already know the shape is sequential
Object.keys(obj).lengthOwn enumerable keysNo Lodash dependency on plain objects
Reflect.ownKeys variantsSymbols + stringsYou need spec-accurate key listings beyond lodash defaults

Pitfalls to avoid

Sparse

Arrays with holes

size trusts length—holes still inflate numeric spans exactly like native arrays.

Semantics

Hidden versus visible keys

Non-enumerable or symbol keys may be skipped—match expectations with Object.getOwnPropertyDescriptors when auditing.

Unicode

Grapheme clusters

Complex emoji sequences count as multiple UTF-16 units—pair with segmenters when UX cares about visual glyphs.

❓ FAQ

No—it inspects length or enumerable keys and returns a number.
Lodash treats nullish collections as empty rather than throwing.
For plain objects it mirrors enumerable own string keys—same spirit as _.keys.
For arrays and array-like values size reads length; objects lack length so keys are counted instead.
Lodash 4 reports their element counts via built-in size properties when passed directly.

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.
Did you know?

_.size(null) and _.size(undefined) both resolve to 0—Lodash treats absent collections as empty instead of throwing like some native accessors.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful