Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Spread and rest vs direct read and write
(version: 0)
Comparing performance of:
rest and spread vs direct assignment
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var myProps = {hasOnReferenceClick: true, isWidthSmallerThanVisible: false, label: 'label', className: 'className', fn: () => true};
Tests:
rest and spread
const {label, ...props} = myProps; const rest = {...props};
direct assignment
const label = myProps.label; const rest = {a: myProps.hasOnReferenceClick, b: myProps.isWidthSmallerThanVisible, c: myProps.className, d: myProps.fn}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
rest and spread
direct 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/130.0.0.0 Safari/537.36 Edg/130.0.0.0
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
rest and spread
9568811.0 Ops/sec
direct assignment
5768286.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Overview** The benchmark measures two approaches to destructure JavaScript objects: 1. **Rest Spread Syntax (Spread)**: Using the spread operator (`...`) to create a new object from an existing one, with all properties except `label` preserved. 2. **Direct Assignment**: Assigning individual properties of the original object (`myProps`) to separate variables. **What's being tested** The benchmark is comparing the performance of these two approaches on a specific test case, which involves deconstructing the `myProps` object and assigning its values to variables. **Options Compared** Two options are compared: 1. **Rest Spread Syntax (Spread)**: This approach uses the spread operator (`...`) to create a new object with all properties except `label` preserved. 2. **Direct Assignment**: This approach assigns individual properties of the original object (`myProps`) to separate variables. **Pros and Cons** * **Rest Spread Syntax (Spread)** + Pros: - More concise and expressive - Can be faster for large objects, as it avoids the overhead of creating multiple assignments + Cons: - May be less readable or maintainable for complex objects or developers unfamiliar with this syntax * **Direct Assignment** + Pros: - Easier to understand and debug for some developers - Allows more control over individual properties + Cons: - More verbose and less concise than the spread operator - May be slower for large objects due to the overhead of multiple assignments **Library and Special JS Feature** There is no explicit library used in this benchmark. However, JavaScript has a special feature called "rest parameters" (introduced in ECMAScript 2015) that allows for variable numbers of arguments to be passed to a function. In this case, the `...props` syntax in the spread operator uses rest parameters to destructure the object. **Other Considerations** Other factors that might affect performance include: * **Object size**: Larger objects may benefit more from the spread operator due to its potential speed advantages. * **Browser support**: Different browsers may have varying levels of support for rest parameters and spread operators, which could impact benchmark results. **Alternatives** If you're interested in exploring alternative approaches or libraries, consider these options: 1. **Other destructure methods**: You can use other methods like `Object.assign()` or `JSON.parse(JSON.stringify(obj))` to achieve similar results. 2. **Alternative object creation libraries**: Libraries like Lodash or Ramda offer more comprehensive set of utility functions for working with objects and arrays. For this specific benchmark, the choice between rest spread syntax and direct assignment ultimately depends on your personal preference, coding style, and performance requirements.
Related benchmarks:
lodash assign vs spread operator
Array push vs spread operator
Spread vs push array
JS array spread operator vs push
delete vs spread need for speed
Comments
Confirm delete:
Do you really want to delete benchmark?