Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript spread operator check
(version: 0)
Comparing performance of:
Using the spread operator vs Using direct assignment
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const obj1 = { a: "a" }; const obj2 = { b: "b" }; window.obj1 = obj1; window.obj2 = obj2;
Tests:
Using the spread operator
const finalObject = { c: "a", ...obj1, ...obj2 };
Using direct assignment
const finalObject = { c: "a" } finalObject.a = obj1.a finalObject.b = obj2.b
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 direct assignment
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Using the spread operator
6859493.5 Ops/sec
Using direct assignment
8469316.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this JavaScript benchmark. **Benchmark Definition** The benchmark is designed to measure the performance difference between two approaches: 1. Using the spread operator (`...`) to merge objects 2. Direct assignment of individual properties to create a new object **Options Compared** We have two options being compared: * **Using the spread operator**: This approach uses the spread operator (`...`) to merge `obj1` and `obj2` into a single object, `finalObject`. The syntax for this is: `{ ...obj1, ...obj2 }` * **Direct assignment**: This approach assigns individual properties from `obj1` and `obj2` directly to `finalObject`, like so: `finalObject.a = obj1.a; finalObject.b = obj2.b` **Pros and Cons** **Using the spread operator:** Pros: * Concise and readable syntax * Less prone to errors due to explicit merging of properties Cons: * Might be slower due to additional object creation and merging **Direct assignment:** Pros: * Fast and lightweight, as only individual properties need to be assigned * Easy to understand for developers familiar with direct property assignments Cons: * More verbose syntax, potentially leading to errors if not done correctly * Less readable than the spread operator approach **Other Considerations** Both approaches have their trade-offs in terms of performance and readability. The spread operator approach is generally considered more modern and concise, but might incur a slight performance penalty due to object creation and merging. **Library Used** The benchmark uses the `window` object to assign variables `obj1` and `obj2` as global variables. This is likely done for simplicity and ease of use in the test case. **Special JS Feature or Syntax** This benchmark does not rely on any special JavaScript features or syntax beyond what's considered standard at this point (ES6+). If you were to add a feature like `const` declarations, arrow functions, or async/await, it might impact the benchmark results. However, since the test cases are relatively simple, these features do not appear to be relevant here. **Alternatives** Other alternatives for creating objects with merged properties could include: * Using the `Object.assign()` method: `finalObject = Object.assign({}, obj1, obj2)` * Using a library like Lodash, which provides a `merge` function for merging objects * Using a template literal or string interpolation to build the final object
Related benchmarks:
JavaScript spread operator vs Object.assign performance 22477
JavaScript spread operator vs Object.assign performance 22478
Object spread
JavaScript spread operator vs Object.assign vs direct performance
Comments
Confirm delete:
Do you really want to delete benchmark?