Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread vs Object.assign vs Object.keys and reduce
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign vs Not merging
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
Using the spread operator
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = { ...firstObject, ...secondObject };
Using Object.assign
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = Object.assign(firstObject, secondObject);
Not merging
const firstObject = { sampleData: 'Hello world' } const secondObject = { moreData: 'foo bar' } const finalObject = Object.keys(firstObject).reduce((acc, key) => { acc[key] = firstObject[key] return acc }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Using the spread operator
Using Object.assign
Not merging
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):
**Benchmark Overview** The provided JSON represents a JavaScript benchmark test case at MeasureThat.net. The test compares the performance of three different methods to merge two objects: using the spread operator (`...`), `Object.assign()`, and a custom implementation without merging (`Not merging`). **Options Compared** 1. **Using the spread operator (`...`)**: * This method uses the rest operator (`...`) to merge the properties of two objects into a new object. * Pros: concise, readable, and efficient for simple cases. * Cons: may not work correctly with non-plain object types (e.g., arrays), and its performance can vary depending on the browser's engine. 2. **`Object.assign()`**: * This method uses the `assign()` function to merge the properties of two objects into a new object. * Pros: widely supported, including in older browsers, and works with non-plain object types. * Cons: can be less efficient than using the spread operator for simple cases, and its performance may vary depending on the browser's engine. 3. **`Not merging`**: * This method creates a new object by concatenating the properties of two objects using `reduce()`. * Pros: allows for more control over the merge process, but can be less efficient than other methods for simple cases. * Cons: may require more code and is not as concise as other methods. **Library** None of the benchmark test cases use any external libraries. The tests rely solely on native JavaScript features and built-in functions like `Object.assign()`. **Special JS Feature/Syntax** The test uses the rest operator (`...`) introduced in ECMAScript 2018, which allows for more concise object merging. Additionally, it uses the `reduce()` function, which is a built-in JavaScript method that iterates over an array or other iterable and applies a callback function to each element. **Other Alternatives** If you want to explore alternative methods for merging objects in JavaScript, consider using: 1. **Lodash's `merge()` function**: A popular utility library that provides a robust implementation of object merging. 2. **Underscore.js's `_merge()` function**: Another utility library that offers an efficient and flexible way to merge objects. 3. **A custom implementation using loops or recursion**: While not as concise or readable as the above methods, you can write your own object merging function using JavaScript's built-in features. Keep in mind that while these alternatives may offer more control or flexibility, they might also come with a performance overhead or require more code to implement.
Related benchmarks:
object assign vs object spread on growing objects
Reduce Object.assign vs spread vs Map vs Function assignment
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
JavaScript spread operator vs Object.assign performance for two targets
Comments
Confirm delete:
Do you really want to delete benchmark?