let bar = Object.create(Object.prototype);
let foo = new Object();
let bax = {};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Object.create | |
new | |
simple |
Test name | Executions per second |
---|---|
Object.create | 1286338304.0 Ops/sec |
new | 1525710080.0 Ops/sec |
simple | 1458555136.0 Ops/sec |
I'd be happy to explain the benchmark being measured.
What is being tested?
The benchmark measures the performance of three different ways to create objects in JavaScript: new
, Object.create
, and a "simple" approach using an empty object literal {}
.
Options compared
The benchmark compares the performance of these three approaches because they have different characteristics that can impact execution speed:
new Object()
: This creates a new, empty object with its own prototype chain. It's a simple and straightforward way to create an object.Object.create(Object.prototype)
: This method creates a new object with the prototype of Object.prototype
. This approach allows for more control over the prototype chain, but can be slower than using the new
operator.{}
: This creates a new object by simply creating an empty object literal. This is often considered the fastest way to create an object in JavaScript.Pros and cons of each approach
new Object()
:Object.create(Object.prototype)
:new
, especially for simple objects.{}
:Library usage
None of the test cases use any external libraries.
Special JavaScript features or syntax
The benchmark does not use any special JavaScript features or syntax that might affect its results. It only uses standard ECMAScript syntax.
Other alternatives
If you're interested in optimizing object creation, you may also consider using techniques like:
Object.assign()
to create new objects from existing onesObject.create()
with a custom prototype objectHowever, for simple cases, the new Object()
and simple {}
approaches are usually sufficient.