Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reflect.set vs Object.assign vs Direct assignment vs Spread operator
(version: 0)
Comparing performance of:
Reflect.set vs Object.assign vs Direct assignment vs Spread operator
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var numbers = Array(1000).fill(null).map((x, i) => i.toString());
Tests:
Reflect.set
numbers.reduce((acc, n) => Reflect.set(acc, n, n) && acc, {});
Object.assign
numbers.reduce((acc, n) => Object.assign(acc, { [n]: n }), {});
Direct assignment
numbers.reduce((acc, n) => { acc[n] = n; return acc }, {});
Spread operator
numbers.reduce((acc, n) => acc = {...acc, [n]: n }, {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Reflect.set
Object.assign
Direct assignment
Spread operator
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Reflect.set
23808.5 Ops/sec
Object.assign
1141.9 Ops/sec
Direct assignment
154860.4 Ops/sec
Spread operator
8919.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition and Test Cases** The benchmark measures the performance of three different ways to assign values to an object: 1. **Reflect.set**: Uses the `Reflect.set` method to set properties on an object. 2. **Object.assign**: Uses the `Object.assign` method to create a new object and assign properties to it. 3. **Direct assignment**: Simply assigns values to the properties of an object directly. 4. **Spread operator**: Uses the spread operator (`...`) to create a new object with assigned properties. Each test case uses the `reduce` method to iterate over an array of strings (numbers) and perform the corresponding operation on an empty object. **Options Compared** The benchmark compares the performance of these four approaches in terms of execution speed, which is measured in executions per second. **Pros and Cons of Each Approach** * **Reflect.set**: This approach uses a native method to set properties on an object. It's likely to be fast since it's implemented in native code. However, it may require additional setup and imports. * **Object.assign**: This approach creates a new object using `Object.assign` and then assigns properties to it. It's generally safe and widely supported but might incur some overhead due to the creation of a new object. * **Direct assignment**: Assigning values directly is simple and fast, as it avoids any function calls or object creations. However, it may be less efficient than using built-in methods like `Reflect.set` or `Object.assign`. * **Spread operator**: The spread operator creates a new object with assigned properties, similar to `Object.assign`. It's concise but might incur some overhead due to the creation of a new object. **Library and Special JS Feature** None of these approaches rely on specific JavaScript libraries. However, they do utilize native methods like `Reflect.set` and `Object.assign`, which are part of the ECMAScript standard. **Other Considerations** When choosing an approach, consider the trade-offs between performance, readability, and maintainability: * If performance is critical, using built-in methods like `Reflect.set` or `Object.assign` might be a better choice. * For conciseness and readability, the spread operator (`...`) can be a good option for simple assignments. * Direct assignment is suitable when you need to assign values quickly without any overhead. **Alternatives** Other alternatives to these approaches include: * Using functions like `forEach` or `map` to iterate over an array and create objects. * Utilizing library functions like Lodash's `assignIn` or Ramda's `assoc`. * Implementing custom assignment logic using loops or recursion. Keep in mind that these alternatives might incur additional overhead due to function calls, object creations, or other operations. By understanding the strengths and weaknesses of each approach, you can choose the most suitable method for your specific use case.
Related benchmarks:
JavaScript spread operator vs Object.assign performance for cloning
object.assign vs spread to create a copy
Reflect.set vs Object.assign vs Direct assignment
object.assign vs spread operator for shallow copying large objects 2
Comments
Confirm delete:
Do you really want to delete benchmark?