Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reflect.set vs Object.assign vs Direct assignment
(version: 0)
Comparing performance of:
Reflect.set vs Object.assign vs Direct assignment
Created:
5 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 }, {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Reflect.set
Object.assign
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; rv:137.0) Gecko/20100101 Firefox/137.0
Browser/OS:
Firefox 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Reflect.set
10343.0 Ops/sec
Object.assign
519.2 Ops/sec
Direct assignment
88606.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data for MeasureThat.net, which measures JavaScript microbenchmarks. **Benchmark Definition** The benchmark tests three different approaches to assign values to an object: 1. **Direct assignment**: `numbers.reduce((acc, n) => { acc[n] = n; return acc }, {})` 2. **Reflect.set**: `numbers.reduce((acc, n) => Reflect.set(acc, n, n) && acc, {})` 3. **Object.assign**: `numbers.reduce((acc, n) => Object.assign(acc, { [n]: n }), {})` **Options Compared** The three approaches differ in how they assign values to the object: * **Direct assignment** uses a simple property access (`acc[n] = n`) to set the value of each key. * **Reflect.set** uses the `Reflect.set` method to dynamically set properties on the object. This method is similar to direct assignment but provides additional features, such as ability to check if a property exists before setting it. * **Object.assign** uses the `Object.assign` method to merge two objects. In this case, it's used to assign individual values to each key in the source array. **Pros and Cons** Here are some pros and cons of each approach: * **Direct assignment**: Simple and efficient, but may not be suitable for complex assignments or when using existing properties. + Pros: Fast, simple + Cons: May lead to property collisions if keys already exist * **Reflect.set**: Provides additional features like checking if a property exists before setting it. However, the method is more verbose than direct assignment. + Pros: Flexible, can check for existing properties + Cons: Verbose, may be slower due to dynamic property checks * **Object.assign**: Easy to use and efficient, but requires creating an object with existing keys first. + Pros: Easy to use, efficient + Cons: Requires extra setup (creating an object with existing keys) **Library/Feature Usage** None of the benchmark cases explicitly uses any libraries or special JavaScript features. However, `Reflect.set` is a built-in method in JavaScript's Reflect API. **Other Considerations** When choosing between these approaches, consider the specific requirements of your application: * If simplicity and speed are crucial, direct assignment might be the best choice. * If flexibility and ability to handle existing properties are necessary, Reflect.set could be a better option. * If ease of use and performance are key, Object.assign is a good choice. **Alternative Approaches** Other alternatives for object assignment in JavaScript include: * Using `forEach` or `map` with the spread operator (`{...obj}`) to create a new object * Using a library like Lodash's `assignIn` method to handle nested objects Keep in mind that these alternatives may have different performance characteristics and use cases compared to the approaches tested in this benchmark.
Related benchmarks:
Dynamic property assignment vs Object.assign
Direct property assignment vs defineProperty
Object.assign() vs Reflect.set()
Reflect.set vs Object.assign vs Direct assignment vs Spread operator
Comments
Confirm delete:
Do you really want to delete benchmark?