Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
deepmerge vs Object.assign
(version: 1)
Benchmark.js
Comparing performance of:
deepmerge vs Object.assign vs Spread operator
Created:
5 years ago
by:
Registered User
Go to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/deepmerge@4.2.2/dist/umd.js"></script>
Tests:
deepmerge
var a = { a: 'oh', b: 'my', c: { a: 'a', b: { c: 'c' } } }; var b = { c: { b: { d: 'a' }, c: { d: 'd' } } }; var c = deepmerge({}, a, b);
Object.assign
var a = { a: 'oh', b: 'my', c: { a: 'a', b: { c: 'c' } } }; var b = { c: { b: { d: 'a' }, c: { d: 'd' } } }; var c = Object.assign({}, a, b);
Spread operator
var a = { a: 'oh', b: 'my', c: { a: 'a', b: { c: 'c' } } }; var b = { c: { b: { d: 'a' }, c: { d: 'd' } } }; var c = {...a, ...b};
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
deepmerge
Object.assign
Spread operator
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Spread operator
16.6M/s
Object.assign
7.2M/s
deepmerge
489K/s
View exact numbers
Test name
Executions per second
✓
Spread operator
16,551,564 Ops/sec
Object.assign
7,171,968 Ops/sec
deepmerge
489,254 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of three approaches to merge two objects in JavaScript: 1. `deepmerge` 2. `Object.assign` 3. Spread operator (`...`) **Options Compared** * `deepmerge`: A library that provides a recursive merge function, which can handle complex object structures. * `Object.assign`: A built-in method for merging objects, but it only works with shallow object merges and doesn't support deep recursion. * Spread operator: A modern JavaScript syntax that allows for efficient object merging. **Pros and Cons of Each Approach** 1. **`deepmerge`**: * Pros: + Handles complex object structures through recursion. + Supports both shallow and deep merges. * Cons: + Requires a separate library, adding dependency overhead. 2. **`Object.assign`**: * Pros: + Built-in method, no additional dependencies required. + Fast and efficient for shallow merges. * Cons: + Limited to shallow object merges (can't handle recursive properties). 3. **Spread operator (`...`)**: * Pros: + Modern JavaScript syntax, easy to understand and use. + Fast and efficient for merging objects with similar structure. * Cons: + Only supports shallow merges; can lead to unexpected behavior if not used carefully. **Library Usage** The `deepmerge` library is used in the benchmark. It's a popular choice for handling complex object merges, but it adds an extra dependency compared to `Object.assign`. **Special JS Feature/Syntax** None of the tested approaches rely on any special JavaScript features or syntax beyond what's standard in modern browsers. **Benchmark Result Interpretation** The benchmark results show that: * The spread operator (`...`) performs the best (fastest), indicating it's a good choice for shallow object merges. * `Object.assign` is faster than `deepmerge`, but still slower than the spread operator. This suggests that while `Object.assign` is fast, its limited functionality might make it less suitable for complex object merges. * `deepmerge` provides the best performance when dealing with deep object structures, making it a good choice for cases where complexity requires more advanced merging logic. **Alternatives** Other alternatives to consider include: 1. **Lodash.merge**: Another popular library that offers more features and flexibility than `deepmerge`, but might come at a performance cost due to its additional complexity. 2. **Object merge libraries like merge-deep or immer**: These libraries offer more advanced merging capabilities, but might require careful consideration of their impact on performance. 3. **Native object merge approaches with recursive functions**: Implementing a custom recursive function for merging objects can be efficient and flexible, but requires manual handling of complexity and edge cases. Keep in mind that these alternatives will have different trade-offs depending on your specific use case and requirements.
Related benchmarks
lodash merge vs deepmerge.all
lodash merge vs deepmerge latest CDN
lodash merge vs deepmerge vs Object.assign
lodash vs deepmerge vs deepmerge-ts
My lodash vs deepmerge vs deepmerge-ts
Comments
Confirm delete:
Do you really want to delete benchmark?