const object = Object.create(null);
object.a = 1;
object.b = 2;
const object = {a: 1, b: 2};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.create | |
Object literal |
Test name | Executions per second |
---|---|
Object.create | 8044307.0 Ops/sec |
Object literal | 282481248.0 Ops/sec |
Let's dive into the details of this benchmark.
Benchmark Description The benchmark compares two approaches to creating an object in JavaScript:
Object.create(null)
{a: 1, b: 2}
)These are two different ways to create objects in JavaScript, and the benchmark aims to measure their performance.
Test Cases
There are two test cases:
Object.create
The script creates an object using Object.create(null)
and then sets two properties on it (a
and b
):
const object = Object.create(null);
object.a = 1;
object.b = 2;
This approach creates a new, empty object with no prototype chain (i.e., null
as its prototype). The Object.create()
method is then used to create a new object that inherits from the specified prototype (null
). Finally, two properties are set on this new object.
The script creates an object using the object literal syntax:
const object = {a: 1, b: 2};
This approach directly creates a new object with the specified properties (a
and b
) without any explicit prototype chain setup.
Library/Feature Used
No external libraries are used in this benchmark. However, the Object.create()
method is a built-in JavaScript function.
Special JS Features or Syntax
None mentioned.
Pros/Cons of Different Approaches
Pros:
Cons:
Object.create()
)Pros:
Object.create()
since it avoids an additional function callCons:
Other Alternatives
Other ways to create objects in JavaScript include:
new
keyword followed by an object constructor functionObject()
function (e.g., const obj = new Object()
)However, these alternatives are not directly compared in this benchmark.