Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator vs Object.assign to new object performance
(version: 0)
Comparing performance of:
Using the spread operator vs Using Object.assign
Created:
3 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);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using the 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 dive into the explanation of the provided benchmark. **Benchmark Definition:** The benchmark is designed to measure the performance difference between two approaches: using the spread operator (`...`) and `Object.assign` methods to merge two objects into a new object. **Options Compared:** 1. **Spread Operator (Spreading)**: * Syntax: `{ ...a, ...b }` * Purpose: Creates a new object with properties from both `a` and `b`. * Pros: + Concise and expressive syntax. + Supports merging objects with arrays and other types of values. * Cons: + May incur additional overhead due to the creation of a new array or other data structure. 2. **Object.assign()**: * Syntax: `Object.assign(target, source)` * Purpose: Merges properties from one or more sources into a target object. * Pros: + Wide support and compatibility across browsers and Node.js versions. + Flexibility to specify options for the assignment (e.g., `Object.assign(target, { ... })`). * Cons: + Verbose syntax compared to spreading. + May require additional processing steps. **Library Used:** None. The benchmark only uses built-in JavaScript features and does not rely on external libraries. **Special JS Feature or Syntax:** The benchmark utilizes the spread operator (`...`) feature, which was introduced in ECMAScript 2018 (ES2018). This feature allows for concise object merging using the syntax `{ ...a, ...b }`. **Benchmark Preparation Code:** The provided `Script Preparation Code` is empty, which means that no specific setup or initialization code is required to run the benchmark. **Other Alternatives:** 1. **JSON Merge Patch (JMP)**: This approach uses a JSON merge patch format to represent the changes between two objects. 2. **Object Merging using `for...in` loop**: Another manual approach can be used, where you iterate over each property of one object and assign it to the target object if the key is not present in the other object. Keep in mind that these alternatives are less concise and less expressive than the spread operator or `Object.assign()`, but they might offer better performance in certain scenarios.
Related benchmarks:
object assign vs object spread on growing objects
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)
JavaScript spread operator vs Object.assign performance test number 99
Comments
Confirm delete:
Do you really want to delete benchmark?