Lodash _.once() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Function

Definition

Creates a function that is restricted to invoking func once. Repeat calls return the value of the first invocation. Part of Lodash 4.x Function (since 0.1.0).

Pass the required arguments shown below. Most helpers return a new function you call later.

Syntax

Call _.once. Start with this line.

Lodash
_.once(func)
CallWhat it does
_.once(func)Primary signature.
_.once(func)Wrap or transform a function.

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

Library declarations
Lodash
_.once(func)

Parameters

ParameterDescription
funcRequired. Function invoked on the first call only

Return Value

Return ValueDescription
*Wrapper that freezes after first result

Lodash Examples

Example 1 — Official: Initialize Once

Call the wrapper twice; the inner function runs once.

Lodash
var calls = 0;

function createApplication() {
  calls += 1;
  return "app-ready";
}

var initialize = _.once(createApplication);

initialize();
initialize();

// calls → 1
Try It Yourself

How It Works

  1. Wrap. _.once returns initialize.
  2. First call. Runs createApplication → calls = 1.
  3. Second call. Skips the body; counter stays 1.
  4. Use case. App boot, config load, one-time listeners.

Example 2 — First Return Value Is Reused

Every later call gets the same stored result.

Lodash
var getId = _.once(function () {
  return Math.floor(Math.random() * 1000);
});

var a = getId();
var b = getId();

a === b;
// → true
Try It Yourself

How It Works

  1. First call. Random id is computed and stored.
  2. Second call. Same number comes back from the cache.
  3. Equality. a === b is always true.
  4. Note. Reloading the page creates a new wrapper — a new id.

Example 3 — Later Arguments Do Not Re-run

Only the first call’s arguments reach func.

Lodash
var greet = _.once(function (name) {
  return "Hello, " + name;
});

greet("Ada");
// → "Hello, Ada"

greet("Grace");
// → "Hello, Ada"   // still the first result
Try It Yourself

How It Works

  1. First. name is "Ada" → stored greeting.
  2. Second. Wrapper is called with "Grace".
  3. Skip. Inner function never sees "Grace".
  4. Need keys? Use _.memoize instead.

Example 4 — _.once vs _.before(2, …)

Both allow one real run; before generalizes to n−1 runs.

Lodash
var n1 = 0;
var n2 = 0;

var a = _.once(function () { return ++n1; });
var b = _.before(2, function () { return ++n2; });

a(); a();
// n1 → 1

b(); b();
// n2 → 1
Try It Yourself

How It Works

  1. once. One invocation of the inner function.
  2. before(2). Allows the first 2 − 1 = 1 run — same count here.
  3. Generalize. before(4, fn) would allow three runs.
  4. Pick. Prefer once when the intent is literally “only once”.

Example 5 — _.once vs _.memoize

Memoize still runs for new keys; once never does after the first call.

Lodash
var onceCalls = 0;
var memoCalls = 0;

var o = _.once(function (n) {
  onceCalls += 1;
  return n * 2;
});

var m = _.memoize(function (n) {
  memoCalls += 1;
  return n * 2;
});

o(2); o(3);
// onceCalls → 1, both return 4

m(2); m(3);
// memoCalls → 2, returns 4 then 6
Try It Yourself

How It Works

  1. once. Second call with 3 still returns 4 from the first run.
  2. memoize. Key 3 is new → runs again → 6.
  3. Rule. once = one result total; memoize = one result per key.
  4. Pick. Init/setup → once; expensive pure lookups → memoize.

Browser & Runtime Support

_.once is a Lodash 4.x Function helper (since 0.1.0). It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash. There is no single native Function.once; Lodash remains the portable one-shot wrapper.

✓ Stable · Lodash 4.x

_.once()

Available in [email protected] full builds and the lodash/once 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
_.once Lodash 4.x+

Bottom line: Safe for production once Lodash is loaded. Prefer import once from "lodash/once" for smaller builds. Ideal for app boot, config loaders, and singleton setup.

Frequently Asked Questions

_.once(func) returns a wrapper that invokes func only on the first call. Later calls return that same first result without running func again.
The wrapper still receives them, but after the first run func is not called — only the stored return value is returned.
memoize caches many results keyed by argument. once remembers exactly one result for the whole wrapper, ignoring later argument differences.
_.before(n, func) lets func run for the first n−1 calls. _.once(func) is the same idea as before(2, func) — one real run.
Yes. Lodash invokes func with the this binding and arguments of the created function on that first call.
Use import once from "lodash/once" or require("lodash/once") for tree-shaking. Full build: import _ from "lodash" then _.once.

Next: _.overArgs()

Create a function that transforms each argument before calling the original.

_.overArgs() 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