Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object spread operator vs property assignment
(version: 0)
Comparing performance of:
Spread operator vs Property assignment
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
Spread operator
const obj = { a: 1, b: 'two', c: true }; const objExt = { ...obj, b: 2 };
Property assignment
const obj = { a: 1, b: 'two', c: true }; obj.b = 2;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread operator
Property assignment
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Spread operator
86145904.0 Ops/sec
Property assignment
220067696.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The provided JSON defines two individual test cases: 1. **Object spread operator vs property assignment** 2. **Property assignment only** These two test cases are designed to compare the performance of JavaScript's object spread operator (`...`) with traditional property assignment (`obj.b = 2;`). **Options being compared** In the first test case, `const objExt = { ...obj, b: 2 };`, the options being compared are: * **Object spread operator**: Using the syntax `{ ...obj, b: 2 }` to create a new object that inherits all properties from `obj` and adds a new property `b` with value `2`. * **Traditional property assignment**: Assigning a new value to an existing property using dot notation (`obj.b = 2;`). In the second test case, `const obj = { a: 1, b: 'two', c: true };\r\nobj.b = 2;`, only traditional property assignment is being tested. **Pros and Cons** Here are some pros and cons of each approach: * **Object spread operator**: + Pros: - More concise and expressive syntax - Can create new objects with a smaller number of lines of code - Can be used to merge multiple objects into one + Cons: - May have performance implications due to the creation of a new object - Not all browsers support it (older versions) * **Traditional property assignment**: + Pros: - Widely supported across all browsers and platforms - Can be used for both assigning and creating new properties + Cons: - More verbose syntax compared to the spread operator **Library usage** In this benchmark, no specific libraries are used. The test cases focus solely on JavaScript's built-in object creation mechanisms. **Special JS feature or syntax** The only special syntax being tested in these benchmarks is the object spread operator (`...`). It was introduced in ECMAScript 2018 (ES9) and has since been widely adopted by modern browsers and JavaScript engines. **Other alternatives** If you want to explore alternative approaches, here are a few options: * **Object creation using `Object.assign()`**: This method creates a new object by copying properties from an existing object. It's similar to the spread operator but may have performance implications due to its overhead. * **Using a library like Lodash**: Lodash provides several functions for working with objects, including `_.cloneDeep()` and `_.assign()`. These functions can be used to create new objects or merge properties from existing objects. Keep in mind that these alternatives might not provide the same level of performance or conciseness as the spread operator or traditional property assignment.
Related benchmarks:
object assign vs object spread on growing objects
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
Spread Operator VS Object.assign performance analysis
Comments
Confirm delete:
Do you really want to delete benchmark?