Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
__proto__: null vs Object create null
(version: 1)
Comparing performance of:
__proto__: null vs Object create null
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
__proto__: null
for(let i=300;i--;)var a={__proto__:null}
Object create null
for(let i=300;i--;)var a=Object.create(null)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
__proto__: null
Object create null
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
__proto__: null
194402.3 Ops/sec
Object create null
455172.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you're examining evaluates the performance of two different methods for creating objects in JavaScript that do not inherit from any prototypes. The methods being compared are: 1. **Using `__proto__: null`** 2. **Using `Object.create(null)`** ### Test Cases 1. **`__proto__: null`**: This syntax is a way to create an object with no prototype. By using `__proto__: null`, you create an object literal that does not inherit from `Object.prototype`, meaning it won't have access to methods such as `toString` or other prototype methods. ```javascript for(let i=300; i--;) var a = {__proto__: null}; ``` 2. **`Object.create(null)`**: This method is a standard JavaScript function that creates a new object with the specified prototype object and properties. When `null` is passed in, it creates an object with no prototype. This approach is widely recognized for its clarity and intent. ```javascript for(let i=300; i--;) var a = Object.create(null); ``` ### Performance Results From the benchmark results, we can observe the following executions per second for each method: - **`Object.create(null)`**: Approximately **93,990 executions per second** - **`__proto__: null`**: Approximately **60,303 executions per second** This indicates that `Object.create(null)` is significantly faster in this scenario. ### Pros and Cons of Each Approach **1. `__proto__: null`** - **Pros**: - Concise and straightforward syntax when creating an object. - **Cons**: - Slightly less performance efficient as shown in the benchmark. - Less conventional and might confuse developers who are less familiar with this syntax. - Potentially increases the risk of inadvertently misunderstanding inheritance and prototypes in JavaScript. **2. `Object.create(null)`** - **Pros**: - Clearly conveys the intent of creating an object without a prototype, enhancing readability. - Offers better performance based on the benchmark results. - Ensures consistency and is a more recognized practice among developers. - **Cons**: - Slightly more verbose than the direct object literal method. ### Other Considerations When choosing between these two approaches, readability and consistency in code should be factored into a developer's decision. While both methods achieve the goal of creating prototype-less objects, using `Object.create(null)` is preferable for long-term maintainability—especially in teams or projects where code clarity is critical. ### Other Alternatives - Another common way of creating plain objects is using the object literal notation (`{}`), but these objects have `Object.prototype` as a prototype by default, making them unsuitable if a non-inheriting object is desired. - Additionally, some developers may consider using ES6 class syntax to define prototypes, but this adds unnecessary complexity for the specific task of creating plain objects. In conclusion, while both methods serve to create objects with no prototype, `Object.create(null)` is generally the preferred choice due to its performance and readability advantages.
Related benchmarks:
object create vs others
Object.create vs {}
Object.create vs Object literal
Object.setPrototypeOf vs Object literal
Object literal vs Object.create(null) v3
javascript new vs Object.create
javascript new vs Object.create 2
javascript new vs Object.create 3
Object.create vs constructor
Comments
Confirm delete:
Do you really want to delete benchmark?