Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.create mutation vs Object.create mutation descriptors vs setPrototypeOf
(version: 0)
Comparing performance of:
Object.create mutation vs Object.create mutation descriptors vs Test Object.assign
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
for (i = 0; i < 10; i++) {}
Tests:
Object.create mutation
const proto = { m1() { return 4324 }, m2() { return "park"} }; const obj = Object.create(proto) obj.age = 65 obj.name = "john"
Object.create mutation descriptors
const proto = { m1() { return 4324 }, m2() { return "park"} }; const obj = Object.create(proto, Object.getOwnPropertyDescriptors({age: 65, name: "john"}));
Test Object.assign
const proto = { m1() { return 4324 }, m2() { return "park"} }; const obj = {age: 65, name: "john"} Object.setPrototypeOf(obj, proto);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.create mutation
Object.create mutation descriptors
Test Object.assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. The benchmark compares three approaches to set properties on an object: 1. **Object.create**: This method creates a new object with the given prototype. The object inherits all the properties from the prototype, including its methods. In this case, we're testing two variants: * `Object.create(proto)`: This sets the prototype of the new object to the provided `proto` without any additional properties. * `Object.create(proto, Object.getOwnPropertyDescriptors({age: 65, name: "john"}))`: This sets the prototype and also creates a new object with additional properties using `Object.getOwnPropertyDescriptors`. The descriptors are used to specify the initial values of the properties. 2. **SetPrototypeOf**: This method sets the prototype of an existing object to a new one. In this case, we're testing: * `const obj = {age: 65, name: "john"}\nObject.setPrototypeOf(obj, proto)`: This sets the prototype of the `obj` object to the `proto` object without any additional properties. 3. **Object.assign**: Although not explicitly stated in the benchmark definition, based on the test case name, it appears that this is actually testing the behavior of setting properties using Object.create and then updating with Object.assign. Now, let's discuss the pros and cons of each approach: * **Object.create**: + Pros: Efficient, as it only sets the prototype without creating new properties. + Cons: If the prototype has methods that need to be called on the new object, those will be inherited from the prototype. Additionally, if the prototype is modified later, these changes won't be reflected in the new object unless you explicitly update its properties using `Object.create` or other means. * **SetPrototypeOf**: + Pros: Allows for more flexibility when updating properties of the existing object after setting its prototype. + Cons: May have performance implications if used excessively, as it involves a separate operation to set the prototype. In this case, however, it's likely not an issue since we're only doing it once. * **Object.create** with additional descriptors: + Pros: Combines efficiency of `Object.create` with the ability to specify initial property values using descriptors. This approach ensures that any changes made to the original object won't affect the new one unless explicitly updated. + Cons: May require more complex setup, as it involves creating an object with descriptors. The use of libraries and special JS features in this benchmark is minimal. The only notable aspect is the use of `Object.getOwnPropertyDescriptors`, which allows for specifying initial property values when setting the prototype using `Object.create`. This feature was introduced in ECMAScript 2015 (ES6) and provides a way to create objects with specific properties. As for alternatives, other approaches to set properties on an object include: * Using `Object.assign` directly: While not explicitly tested here, this would involve creating an empty object or using the spread operator (`{...}`) to merge properties. * Using constructors: This involves creating a constructor function that initializes the object's properties.
Related benchmarks:
Object.assign() vs Reflect.set()
Object.setPrototypeOf vs Object literal
typeof vs instanceof vs constructor vs toString
typeof vs cached typeof (3 scans) (fixed)
javascript new vs Object.create 3
Comments
Confirm delete:
Do you really want to delete benchmark?