Lodash _.sampleSize() method

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

What you’ll learn

  • How _.sampleSize(collection, n) returns up to n pseudo-random elements as a new array.
  • Behavior when n exceeds collection size or when the collection is empty.
  • Differences from single-draw sample and full shuffle permutations.
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Read _.sample() first—same RNG caveats, but this helper draws batches instead of singletons.

  • You know why helper randomness is not cryptographically secure by default.
  • You can open Try-it labs or run snippets locally.

Overview

_.sampleSize packages “give me n winners” logic—lottery seats, spot QA checks, randomized A/B cohort slices—without manually indexing Math.random.

Batch sampling

One call returns an array whose length is min(n, size) for finite collections.

Objects & strings

Sample plain-object values or string characters using the same arity-two signature.

Non-destructive

Outputs are fresh arrays—you never mutate client payloads accidentally.

Syntax

javascript
_.sampleSize(collection, n)
  • collection: array, array-like string, or plain object Lodash can iterate.
  • n: non-negative integer count—results shrink automatically when the pool is smaller.
  • Returns: new array (possibly empty) of sampled members in arbitrary order.
1

Draw two numbers from an array

Each Try-it run can permute differently—Lodash picks distinct indexes into the source array.

javascript
import sampleSize from "lodash/sampleSize";

sampleSize([1, 2, 3, 4], 2);
// → length 2 array; elements ∈ {1,2,3,4}
Try it Yourself
2

Draw two values from a plain object

Returned entries are values only—keys participate solely during enumeration.

javascript
import sampleSize from "lodash/sampleSize";

sampleSize({ alpha: 1, beta: 2, gamma: 3 }, 2);
// → two distinct values from {1,2,3}
Try it Yourself
3

Draw three characters from a string

Duplicate letters can appear twice because each index is unique—even when characters match visually.

javascript
import sampleSize from "lodash/sampleSize";

sampleSize("hello", 3);
// → array of three single-character strings
Try it Yourself

📋 _.sampleSize vs sample, shuffle, manual loops

APIOutcomeBest when
_.sampleSize(collection, n)Up to n picksYou need multiple unique draws without reordering everything
_.sample(collection)Exactly one pickSingleton lottery spins
_.shuffle(collection)Permuted copyYou eventually consume the entire randomized sequence
Manual Fisher-Yates sliceCustom RNG controlYou must reproduce statistical guarantees outside Lodash

Pitfalls to avoid

Crypto

Predictable RNG

Security workloads still demand crypto-grade sources—Lodash stays in UX/random-demo territory.

Assumptions

Expecting sorted results

Results arrive shuffled—sort explicitly when IDs must rise monotonically.

Large n

Silent truncation

When n overshoots pool size you simply receive fewer elements—validate counts in business logic.

❓ FAQ

No—it builds a fresh array of picks while leaving inputs untouched.
Lodash returns at most length samples—for sparse finite collections you still cannot exceed available enumerable slots.
For arrays and object-value pools Lodash draws distinct indexes—duplicate primitives appear only if they existed twice already.
Repeated letters occupy distinct indexes—sampleSize may surface duplicate-looking outputs while indices stayed unique.
No—ORDER mirrors Lodash internal shuffle draws; never rely on ascending IDs unless you sort afterward.

Summary

  • Purpose: _.sampleSize(collection, n) builds a new array with up to n pseudo-random members.
  • Contrast: prefer sample for single draws; plan shuffle when the entire permutation matters.
  • Next: Lodash _.shuffle(), Lodash _.sample() (previous), or collection hub.
Did you know?

When n is larger than the available finite elements (arrays/objects), Lodash returns only what exists—never padded placeholders—mirroring bounded reservoir intuition rather than throwing.

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