for(let i=300;i--;)var a={__proto__:null}
for(let i=300;i--;)var a=Object.create(null)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
__proto__: null | |
Object create null |
Test name | Executions per second |
---|---|
__proto__: null | 54802.8 Ops/sec |
Object create null | 93864.0 Ops/sec |
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:
__proto__: null
Object.create(null)
__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.
for(let i=300; i--;) var a = {__proto__: null};
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.
for(let i=300; i--;) var a = Object.create(null);
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 secondThis indicates that Object.create(null)
is significantly faster in this scenario.
1. __proto__: null
2. Object.create(null)
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.
{}
), but these objects have Object.prototype
as a prototype by default, making them unsuitable if a non-inheriting object is desired.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.