Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavScript spread operator vs Object.assign
(version: 0)
Comparing performance of:
Using spread operator vs Using Object.assign
Created:
8 years ago
by:
Guest
Jump to the latest result
Tests:
Using spread operator
const first = {test: 1}; const second = {hi: 2}; const final = {...first, ...second}
Using Object.assign
const first = {test: 1}; const second = {hi: 2}; const final = Object.assign({}, first, second);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using spread operator
Using Object.assign
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 benchmark and explain what's being tested. **What is being tested?** The provided benchmark compares two approaches for merging objects in JavaScript: 1. Using the spread operator (`{...first, ...second}`) 2. Using `Object.assign()` with an initial object (`const final = Object.assign({}, first, second)`) **Options compared:** * **Spread Operator:** This approach uses the syntax `{...obj1, ...obj2}` to merge two objects, `obj1` and `obj2`. It creates a new object that includes all key-value pairs from both objects. * **Object.assign():** This approach uses the `Object.assign()` method to merge two objects. It takes an initial object (`{}`) as the first argument, followed by one or more source objects. **Pros and Cons:** * **Spread Operator:** + Pros: - More concise and readable syntax. - Creates a new object without modifying the original objects. + Cons: - Can be slower due to object creation overhead. - May not work correctly if used with `const` or `let` declarations (since it creates a new object reference). * **Object.assign():** + Pros: - Faster than spread operator, since it modifies the initial object directly. - Works correctly with `const` and `let` declarations. + Cons: - Less concise syntax. - Modifies the original object if not used carefully. **Library:** None. This benchmark only uses built-in JavaScript features. **Special JS feature or syntax:** The spread operator (`{...obj1, ...obj2}`) is a new feature introduced in ECMAScript 2018 (ES10). It allows for more concise and expressive object merging. **Benchmark preparation code:** Since the `Script Preparation Code` field is empty, it's assumed that the benchmark script simply defines two objects (`first` and `second`) with some properties, and then uses the spread operator or `Object.assign()` to merge them into a new object (`final`). The exact implementation details are not provided. **Other alternatives:** * **Using the `Object.create()` method:** This approach creates a new object using the `Object.create()` method, followed by assigning values from the source objects using `Object.assign()`. ```javascript const final = Object.create(Object.getPrototypeOf(first)); final.test = first.test; final.hi = second.hi; ``` * **Using the `reduce()` method:** This approach uses the `reduce()` method to accumulate properties from both objects into a new object. ```javascript const final = {}; Object.keys(first).forEach(key => final[key] = first[key]); Object.keys(second).forEach(key => final[key] = second[key]); ``` These alternatives are less common and may not be as concise or readable as the spread operator or `Object.assign()`.
Related benchmarks:
JavaScript spread operator vs Object.assign performance v2
JavaScript spread operator vs Object.assign performance without useless assignment
JavaScript spread operator vs Object.assign performance (single addition)
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
Comments
Confirm delete:
Do you really want to delete benchmark?