Lodash _.size() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Collection

Definition

Gets the size of a collection: length for array-like values, or the number of own enumerable string keys for objects. Also supports Map / Set. Part of Lodash 4.x Collection (since 0.1.0).

Pass the required arguments shown below. Lodash Collection helpers usually return a new value — check whether the source is mutated.

Syntax

Call _.size on a collection. Start with this line.

Lodash
_.size(collection)
CallWhat it does
_.size(collection)Primary signature.
_.size(collection)Call with a collection argument.

Prefer import size from "lodash/size" when you want a smaller bundle.

Library declarations
Lodash
_.size(collection)

Parameters

ParameterDescription
collectionOptional. Value to measure (*nullish → 0)

Return Value

Return ValueDescription
*Non-negative integer

Lodash Examples

Example 1 — Official: Array Size

Arrays use .length under the hood.

Lodash
_.size([1, 2, 3]);
// → 3
Try It Yourself

How It Works

  1. Array-like. Lodash detects a length property.
  2. Read length. Three elements → 3.
  3. Same as. [1, 2, 3].length for dense arrays.
  4. Sparse note. Holes still count toward length.

Example 2 — Official: Object Key Count

Plain objects count own enumerable keys.

Lodash
_.size({ a: 1, b: 2 });
// → 2
Try It Yourself

How It Works

  1. Not array-like. Objects use key counting.
  2. Own keys. "a" and "b" → size 2.
  3. Same idea. Object.keys({ a: 1, b: 2 }).length.
  4. Prototype. Inherited keys are not included.

Example 3 — Official: String Size

Strings are measured as array-like character sequences.

Lodash
_.size("pebbles");
// → 7
Try It Yourself

How It Works

  1. String path. Lodash uses its string-size helper.
  2. Count. "pebbles" has seven letters → 7.
  3. ASCII. Matches "pebbles".length here.
  4. Unicode. Complex glyphs may differ from naive .length — test emoji if that matters.

Example 4 — null and undefined

Missing values are treated as empty — size 0.

Lodash
_.size(null);
// → 0

_.size(undefined);
// → 0
Try It Yourself

How It Works

  1. Guard. Lodash checks collection == null first.
  2. Return 0. No throw — safer than null.length.
  3. API data. Handy when a response field may be missing.
  4. Empty check. Pair with _.isEmpty when you care about “no content” broadly.

Example 5 — Empty Array and Object

Empty collections still report size 0.

Lodash
_.size([]);
// → 0

_.size({});
// → 0
Try It Yourself

How It Works

  1. Empty array. length is 0.
  2. Empty object. No own keys → 0.
  3. Consistent. Same result as nullish for “nothing there.”
  4. UI tip. Use _.size(list) === 0 to show an empty state.

Browser & Runtime Support

_.size is a Lodash 4.x Collection helper (since 0.1.0). It runs anywhere Lodash runs — modern browsers (via CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 0.1.0+

_.size()

Available in [email protected] full builds and the lodash/size modular import. Works in evergreen browsers and current Node LTS when Lodash is installed.

100% With Lodash 4.x
Google Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · Modern WebView
Full support
_.size Lodash 4.x+

Bottom line: Safe for production once Lodash is on the page or in your bundle. Prefer import size from "lodash/size" for smaller builds. Not a native browser API — load Lodash first. For arrays-only, .length is enough.

Frequently Asked Questions

_.size(collection) returns the length of array-like values, or the number of own enumerable string keys for plain objects. For Map and Set it returns collection.size.
_.size(null) and _.size(undefined) both return 0 — Lodash treats missing values as empty instead of throwing.
For arrays they match. _.size also works on objects, strings, Map, and Set with one API.
No. For objects it counts own enumerable string keys — same idea as Object.keys(obj).length.
No. It only reads length or keys and returns a number.
Use import size from "lodash/size" or require("lodash/size") for tree-shaking. Full build: import _ from "lodash" then _.size.

Next: _.some()

Check whether any element in a collection passes a predicate.

_.some() tutorial →

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.

10 people found this page helpful