<!--your preparation HTML code goes here-->
'use strict';
class a {a=3;b(){blur()}}
class b {a=3;b(){blur()}}
let obj1 = new a
let obj2 = new a
Reflect.setPrototypeOf(obj2, b.prototype)
obj1.a = 2
obj1.a
delete obj1.a
obj1.b()
'a'in obj1
Object.defineProperty(obj1,'a',{value:2})
obj2.a = 2
obj2.a
delete obj2.a
obj2.b()
'a'in obj2
Object.defineProperty(obj2,'a',{value:2})
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
normal object | |
mutated |
Test name | Executions per second |
---|---|
normal object | 3372198.5 Ops/sec |
mutated | 3417933.8 Ops/sec |
The benchmark defined by the provided JSON compares two different approaches to handling objects in JavaScript: "normal objects" and "mutated objects." Specifically, it examines the performance implications of manipulating properties on these objects.
Classes Defined:
a
and b
are defined, both of which have a property a
initialized to 3
and a method b()
that calls blur()
. The blur()
function is a built-in JavaScript method that is typically used in the context of user interface elements (e.g., input fields) to remove focus. However, its invocation here does not play a significant role in the benchmark's focus on object property manipulation.Objects Creation:
a
are created, obj1
and obj2
. obj2
is set to b.prototype
, making it a "mutated object" with a different prototype chain.Normal Object (obj1
):
a
property (obj1.a = 2
).a
(obj1.a
).a
property (delete obj1.a
).b()
on obj1
.a
exists in obj1
('a' in obj1
).Object.defineProperty
to change the property descriptor of a
.Mutated Object (obj2
):
obj2
with the same sequence:a
, retrieving its value, deleting it, calling b()
, checking for the existence of a
, and modifying its property descriptor.obj2
) had an execution rate of approximately 3,417,934.75 executions per second.obj1
) had a slightly lower execution rate of approximately 3,372,198.5 executions per second.Pros:
Cons:
Pros:
Cons:
Map
or Set
for dynamic data structures might be beneficial when property access patterns are varied and complex.This benchmark serves to highlight the differences in how JavaScript handles object properties and the resulting performance variations depending on the approach taken by developers.