Logo

dev-resources.site

for different kinds of informations.

lodash._merge vs Defu

Published at
1/13/2025
Categories
typescript
opensource
lodash
arrays
Author
ramunarasinga-11
Author
16 person written this
ramunarasinga-11
open
lodash._merge vs Defu

In one of the previous week’s articles, I noticed Defu is used in unbuild source code to merge objects. This got me wondering how this is different to lodash._merge. In this article, we will look at their differences. I created a Codesandbox repository for the purposes of this article. Let’s get started.

lodash._merge

Below is definition picked from official docs.

This method is like _.assign except that it recursively merges own and inherited enumerable string keyed properties of source objects into the destination object. Source properties that resolve to undefined are skipped if a destination value exists. Array and plain object properties are merged recursively. Other objects and value types are overridden by assignment. Source objects are applied from left to right. Subsequent sources overwrite property assignments of previous sources.

Example

var object = {
  'a': [{ 'b': 2 }, { 'd': 4 }]
};

var other = {
  'a': [{ 'c': 3 }, { 'e': 5 }]
};

_.merge(object, other);
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }
Enter fullscreen mode Exit fullscreen mode

Arrays and objects are merged recursively.

So far so good, now let’s look at Defu

I study large open-source projects and provide insights, give my repository a star.

Defu

Below definition is picked from Defu’s npm page.

Assign default properties, recursively. Lightweight and Fast.

Example

import { defu } from "defu";

console.log(defu({ a: { b: 2 } }, { a: { b: 1, c: 3 } }));
// => { a: { b: 2, c: 3 } }
Enter fullscreen mode Exit fullscreen mode

At this point, I would investigate if arrays are merged in defu or if it only deals with objects. Let’s pick the example from lodash._merge and see the results.

In this sandbox example — https://codesandbox.io/p/devbox/yn9ds8, I have setup the defu and lodash._merge and below is a screenshot of my attempt to merge arrays recursively using Defu.

Image description

Defu cannot merge arrays recrusively, I would use Defu stricly to merge objects only. The key difference here is:

// Using lodash._merge
var object = {
  'a': [{ 'b': 2 }, { 'd': 4 }]
};

var other = {
  'a': [{ 'c': 3 }, { 'e': 5 }]
};

_.merge(object, other);
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }] }

// Using the same example in defu
defu(
    {
      a: [{ b: 2 }, { d: 4 }],
    },
    {
      a: [{ c: 3 }, { e: 5 }],
    }
  )
// { a: [ { b: 2 }, { d: 4 }, { c: 3 }, { e: 5 } ] }
Enter fullscreen mode Exit fullscreen mode

What’s the count of array items in these two examplse? lodash._merge has only two items, whereas defu has four items, each item being an object. Something to keep in mind, I guess.

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

I am open to work on interesting projects. Send me an email at [email protected]

My Github — https://github.com/ramu-narasinga

My website — https://ramunarasinga.com

My Youtube channel — https://www.youtube.com/@thinkthroo

Learning platform — https://thinkthroo.com

Codebase Architecture — https://app.thinkthroo.com/architecture

Best practices — https://app.thinkthroo.com/best-practices

Production-grade projects — https://app.thinkthroo.com/production-grade-projects

References:

  1. https://lodash.com/docs/#merge

  2. https://github.com/unjs/unbuild/blob/main/src/build.ts#L93

  3. https://github.com/unjs/defu#readme

opensource Article's
30 articles in total
Open-source projects foster collaboration and innovation, allowing anyone to view, modify, and share the code freely.
Favicon
Memory Management in Operating Systems
Favicon
2025: The Year of Decentralization – How Nostr Will Make You a Standout Developer
Favicon
10 Must-Bookmark Open Source Projects for Developers
Favicon
[Boost]
Favicon
join my project semester simulator
Favicon
340+ Websites every developer should know
Favicon
KDE vs GNOME vs Others: Choosing the Best Linux Desktop Environment in 2025
Favicon
assert in Nodejs and its usage in Grida source code
Favicon
Contribute to `real-to-sim-to-real` in SmilingRobo Open-Source Sprint!
Favicon
Exploring the CNCF Landscape: A Comprehensive Overview of Cloud Native Technologies
Favicon
🎁 20 Open Source Projects You Shouldn't Miss in 2025
Favicon
Any recommendations of open source asset inventory ?
Favicon
Getting Started with the Open Source AI Hackathon
Favicon
Supercharge Your JavaScript Agents with Firecrawl in KaibanJS
Favicon
Top 10 Trending GitHub Repositories, Nov 24 2024
Favicon
Open-Source TailwindCSS React Color Picker - Zero Dependencies! Perfect for Next.js Projects!
Favicon
Procrastinator’s Guide to Glory: Turning Wasted Time Into Career Gold with Open Source
Favicon
Kubernetes Security Best Practices
Favicon
SPL: a database language featuring easy writing and fast running
Favicon
7 Open-Source Tools for Better Website Analytics
Favicon
Train LLM From Scratch
Favicon
Have you ever used `git submodules`?
Favicon
✨ Introducing Tooltip: A Revolutionary Suite of Developer Tools** ✨
Favicon
Becoming An Open Source Maintainer
Favicon
Open-Source React Icon Picker: Lightweight, Customizable, and Built with ShadCN, TailwindCSS. Perfect for Next.js Projects!
Favicon
Enhance Your App's Security with OTP-Agent
Favicon
3 essential elements for Web publishing
Favicon
Pulumi WASM/Rust devlog #3
Favicon
Sign up to our bug bounty platform!
Favicon
lodash._merge vs Defu

Featured ones: