Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.create vs constructor
(version: 0)
Comparing performance of:
Object.create vs Constructor
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Object.create
for(let i = 0;i < 10000000;i++ ){ const test = Object.create(null); }
Constructor
for(let i = 0;i < 10000000;i++ ){ const test = {} }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.create
Constructor
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/605.1.15 (KHTML, like Gecko) Version/15.6.1 Safari/605.1.15
Browser/OS:
Safari 15 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.create
316.2 Ops/sec
Constructor
317.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its results. **What is being tested?** The provided JSON represents two microbenchmarks that compare the performance of `Object.create(null)` and using constructors in JavaScript. Both benchmarks aim to create an object without any prototype chain. **Options compared:** * **`Object.create(null)`**: This method creates a new object with no prototype, effectively making it a bare object (i.e., no inheritance). * **Using constructors (`{}`)**: In this approach, an empty object is created using the `{}` syntax, which also results in a bare object. **Pros and Cons of each approach:** * `Object.create(null)`: + Pros: - It's more explicit and readable than using constructors. - No risk of accidentally creating objects with implicit inheritance (which can lead to unexpected behavior). + Cons: - It may be slightly slower due to the extra function call. - Some older JavaScript engines might not support `Object.create` at all, which could affect compatibility. * Using constructors (`{}`): + Pros: - It's a more concise and familiar syntax for creating objects in many developers' experience. - Modern JavaScript engines generally support it natively. + Cons: - May lead to implicit inheritance if the constructed object is not properly handled (e.g., by setting ` prototypes` property explicitly). - Might be perceived as less readable or maintainable than using `Object.create`. **Other considerations:** * Both approaches are suitable for creating bare objects without any prototype chain, but they serve different purposes: + `Object.create(null)` is often used when you want to create an object with a specific set of properties or methods that should be inherited from the original prototype. + Using constructors (`{}`) is typically used when you need to create a new object and don't care about its inheritance hierarchy. **Library usage:** There are no libraries explicitly mentioned in the provided JSON. However, some JavaScript engines might rely on internal libraries or frameworks that could influence benchmark results. **Special JS feature or syntax:** Neither of the benchmarks specifically utilizes special features like ES6 classes, generators, or async/await. The tests focus solely on basic object creation mechanisms. **Benchmark preparation code and result explanation:** The provided JSON includes two individual test cases: 1. `Object.create(null)`: Creates a new object using `Object.create(null)` to ensure it has no prototype chain. 2. Using constructors (`{}`): Creates an empty object using the `{}` syntax, also resulting in a bare object. The benchmark results show that: * The constructor approach (`{}`) is faster on this specific test case (1.5527950525283813 executions per second), likely due to its concise syntax and native support. * However, `Object.create(null)` performs slightly worse (397.6217041015625 executions per second) in terms of raw execution speed. Keep in mind that these results may not be representative for all JavaScript environments or use cases. **Other alternatives:** In addition to the provided benchmarks, other alternatives for creating bare objects include: * Using a class expression (`new Date()` instead of `Date`) * Utilizing the `Object.assign({}, obj)` method (which also creates an object without inheritance) These alternatives might be worth exploring in specific scenarios or when dealing with compatibility issues.
Related benchmarks:
object create vs others
Object.create(null) vs Object literal
Check object. typeof vs constructor + null check
javascript new vs Object.create 2
javascript new vs Object.create 3
Comments
Confirm delete:
Do you really want to delete benchmark?