Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs spread opertator vs lodash assignIn
(version: 0)
Comparing performance of:
Object.assign() vs SPREAD operator vs Lodash assignIn
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.js"></script>
Script Preparation code:
var foo = {} for (var i=0;i<100;i++) { foo['foo'+i] = i; foo['bar'+i] = i; foo['foobar'+i] = i; } var bar = {} for (var i=0;i<100;i++) { bar['bar'+i] = i; bar['foo'+i] = i; bar['barfoo'+i] = i; }
Tests:
Object.assign()
const x = Object.assign({}, foo, bar)
SPREAD operator
const x = {...foo, ...bar}
Lodash assignIn
const x = _.assignIn({}, foo, bar)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.assign()
SPREAD operator
Lodash assignIn
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking test cases. **What is being tested?** The test measures the performance of three different ways to merge two objects: `Object.assign()`, the spread operator (`...`), and `_.assignIn()` from the Lodash library. The test creates two large objects, `foo` and `bar`, with 100 properties each. **Options compared:** 1. **Object.assign()**: A built-in JavaScript method that merges two or more source objects into a new object. 2. **Spread operator (`...`)**: A syntax feature introduced in ECMAScript 2018 (ES10) for creating a new object from an existing one by spreading its properties onto another object. 3. **Lodash `_.assignIn()`**: A utility function from the Lodash library that merges two objects, where each key in the first object becomes the key in the second object only if it does not already exist. **Pros and cons of each approach:** 1. **Object.assign()**: * Pros: Widely supported, easy to use, and efficient for simple cases. * Cons: Can be slow and less efficient than other methods, especially when dealing with large objects or deep merging. 2. **Spread operator (`...`)**: * Pros: Simple, concise, and fast, but only supports ES10+ browsers and environments. * Cons: Limited browser support, may lead to unexpected behavior if not used carefully. 3. **Lodash `_.assignIn()`**: * Pros: Efficient for deep merging, flexible, and widely supported (as long as Lodash is included in the project). * Cons: Requires including an additional library, may have a slightly higher overhead due to its implementation. **Library usage** The test uses the Lodash library (`lodash.js`) which provides utility functions like `_.assignIn()`. The library allows for more control and flexibility when merging objects, especially with deep merging. However, it requires including the library in the project, which may add extra dependencies. **Special JS features or syntax** The spread operator (`...`) is a new syntax feature introduced in ES10. While it's becoming increasingly popular, its support is not yet universal, and older browsers may not execute this code correctly. In summary, the test compares three different methods for merging objects: `Object.assign()`, the spread operator, and `_.assignIn()` from Lodash. The choice of method depends on factors like performance requirements, browser support, and personal preference.
Related benchmarks:
Object.assign vs SPREAD vs lodash assignIn
Object.assign vs SPREAD vs lodash assignIn
lodash merge vs object.assign vs spread (no intermediate vars)
lodash assign vs object.assign vs spread operator - variable and constant
Comments
Confirm delete:
Do you really want to delete benchmark?