Lodash _.stubObject() Method

Beginner
5 min read
Updated: Nov 2026
5 examples
Util

Definition

This method returns a new empty object. Part of Lodash Util since 4.13.0 (stable — not deprecated).

Note: Every call creates a fresh {}. That differs from _.constant({}), which reuses one shared object.

Syntax

Call _.stubObject. Start with this line.

Lodash
_.stubObject()
CallWhat it does
_.stubObject()Primary signature.
_.stubObject(...)Call the util helper.

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

Library declarations
Lodash
_.stubObject()

Parameters

This method has no parameters beyond those shown in the signature.

Return Value

Return ValueDescription
*New empty object {}

Lodash Examples

Example 1 — Official: Distinct Empty Objects

Build two empty objects that are not the same reference.

Lodash
var objects = _.times(2, _.stubObject);

console.log(objects);
// → [{}, {}]

console.log(objects[0] === objects[1]);
// → false
Try It Yourself

How It Works

  1. Pass by reference. _.stubObject — no trailing ().
  2. Two calls. _.times invokes it twice.
  3. Two objects. Each call returns a new {}.
  4. Check. === is false — not shared.

Example 2 — Direct Call

Get one empty object when you need it immediately.

Lodash
_.stubObject();
// → {}

Object.keys(_.stubObject());
// → []
Try It Yourself

How It Works

  1. Call. Returns a plain empty object.
  2. Keys. Object.keys shows no own properties.
  3. Type. A normal object — safe to assign keys later.
  4. Use. Handy default for option bags and maps.

Example 3 — Independent Mutation

Changing one stub object does not affect another.

Lodash
var a = _.stubObject();
var b = _.stubObject();

a.name = "Ada";

console.log(a);
// → { name: "Ada" }

console.log(b);
// → {}
Try It Yourself

How It Works

  1. Two factories. a and b are different objects.
  2. Mutate a. Add name only on a.
  3. b stays empty. No shared reference leak.
  4. Why it matters. Safe for per-item defaults in loops.

Example 4 — stubObject vs constant({})

Factory of fresh objects vs one shared stub.

Lodash
var fresh = _.times(2, _.stubObject);
fresh[0] === fresh[1];
// → false

var shared = _.times(2, _.constant({}));
shared[0] === shared[1];
// → true
Try It Yourself

How It Works

  1. stubObject. Each slot gets its own {}.
  2. constant({}). Closes over one object for every call.
  3. Shared risk. Mutating shared[0] also changes shared[1].
  4. Choose. Need isolation → stubObject; need one shared bag → constant.

Example 5 — stubObject vs stubArray

Same “fresh empty” idea for objects and arrays.

Lodash
_.stubObject();
// → {}

_.stubArray();
// → []

var arrays = _.times(2, _.stubArray);
arrays[0] === arrays[1];
// → false
Try It Yourself

How It Works

  1. Object stub. Returns {}.
  2. Array stub. Returns [].
  3. Same rule. Each call is a new container.
  4. Pick. Match the empty structure your API expects.

Browser & Runtime Support

_.stubObject is a Lodash Util helper since 4.13.0. It runs anywhere Lodash 4.13+ runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.

✓ Stable · Lodash 4.13.0+

_.stubObject()

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

100% With Lodash 4.13+
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
_.stubObject Lodash 4.13.0+

Bottom line: Safe for production once Lodash 4.13+ is loaded. Prefer import stubObject from "lodash/stubObject" for smaller bundles. Use it when you need a fresh empty object per call.

Frequently Asked Questions

_.stubObject() returns a new empty object {} every time. Official: _.times(2, _.stubObject) → [{}, {}] and objects[0] === objects[1] is false.
No. Each call allocates a fresh {}. Mutating one result does not change another.
_.constant({}) always returns the same object reference. stubObject returns a new {} per call — safer as a factory.
No. Arguments are ignored. _.stubObject({ a: 1 }) still returns a new empty object.
As a default-object factory, with _.times to build many empty buckets, or anywhere an API needs a fresh {} callback.
Use import stubObject from "lodash/stubObject" or require("lodash/stubObject"). Full build: import _ from "lodash" then _.stubObject.

Next: _.stubString()

Return an empty string on every call.

_.stubString() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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