Lodash _.once() method

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

What you’ll learn

  • How _.once runs the inner function exactly one time.
  • How the cached return value behaves when callers pass different arguments afterward.
  • How once compares to _.before, manual flags, and _.memoize.
  • Pitfalls around side effects, this, and expecting per-call recomputation.

Prerequisites

Helpful reads: _.negate(), _.before(), and the Function hub.

  • Higher-order functions: functions that return or consume other functions.
  • Closures: inner functions that retain variables from an enclosing scope.

Overview

_.once(func) builds a wrapper that calls func at most once. The first return value is stored and reused, even when future calls supply different arguments.

Syntax

javascript
_.once(func)
  • func: function to invoke only on the first wrapper call.
  • returns: wrapped function that replays the first computed result.
1

One-time expensive computation

Share a single initializer across modules without manual booleans.

javascript
import once from "lodash/once";

const loadConfig = once(() => {
  console.log("fetching config...");
  return { apiUrl: "https://example.test" };
});

loadConfig(); // logs once
loadConfig(); // silent — returns cached object
2

Stable identity across calls

When func constructs an object, every caller receives the same reference afterward.

javascript
import once from "lodash/once";

const getSingleton = once(() => ({ createdAt: Date.now() }));

const a = getSingleton();
const b = getSingleton();

Object.is(a, b); // true
3

Arguments after the first call

Later arguments no longer reach func—they exist only on the first invocation.

javascript
import once from "lodash/once";

const summarize = once((label, value) => ({
  label,
  value
}));

summarize("first", 1); // { label: "first", value: 1 }
summarize("ignored", 99); // still first object — inner func never reruns

📋 _.once vs _.before vs manual flags

ApproachBest forTrade-off
_.onceExactly one execution shared by every callerAlways ignores recomputation for fresh inputs
_.before(n, func)Allow multiple runs until a cutoffRequires tuning n and mental model of counters
Manual flagFull inline controlEasy to forget resets or thread-safety in async flows

Pitfalls to avoid

Inputs

Expecting per-call recomputation

Once never recomputes; reach for memoize or plain functions when inputs should produce distinct outputs.

this

Lost component context

If func relies on dynamic this, bind explicitly—otherwise first-call context is baked into behavior unpredictably.

Effects

Hidden one-shot side effects

Observers expect idempotent wrappers; document when listeners mutate external state only during the first hit.

❓ FAQ

A wrapper function that invokes func the first time it is called, remembers that return value, and returns the same value on every subsequent invocation.
Yes. Lodash still invokes the wrapper with whatever arguments you pass, but after the first run inner func is no longer called—only the stored result is returned.
before(n, func) lets func run up to n - 1 times and freezes on the last computed result once the threshold trips. once is the specialization equivalent to needing exactly one execution for every caller sharing that wrapper.
No. Unlike memoize, once ignores distinct argument histories—it remembers exactly one outcome total.
Use import once from "lodash/once" so bundlers can tree-shake unused Lodash.

Summary

Did you know?

Lodash _.once(func) stores the value returned from the first successful invocation and replays it for every later call—so expensive setup runs exactly once while callers keep passing updated arguments into the wrapper.

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