const obj = {a: 1, b: 2, c: 3, d: 4};
const sealedObj = Object.seal({a: 1, b: 2, c: 3, d: 4, __proto__: null});
let v = null;
v = sealedObj.a;
v = sealedObj.b;
v = sealedObj.c;
v = sealedObj.d;
v = obj.a;
v = obj.b;
v = obj.c;
v = obj.d;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Sealed object access | |
Ordinary object access |
Test name | Executions per second |
---|---|
Sealed object access | 3210107.8 Ops/sec |
Ordinary object access | 3223505.5 Ops/sec |
The benchmark described compares the performance of accessing properties from two types of JavaScript objects: a standard (ordinary) object and a sealed object.
Benchmark Setup:
obj
: An ordinary JavaScript object.sealedObj
: A sealed object created using Object.seal()
.a
, b
, c
, and d
.Test Cases:
a
, b
, c
, and d
from the ordinary object.v = obj.a;
v = obj.b;
v = obj.c;
v = obj.d;
v = sealedObj.a;
v = sealedObj.b;
v = sealedObj.c;
v = sealedObj.d;
Ordinary Object:
Sealed Object:
Object.freeze()
, which not only prevents adding new properties but also ensures existing properties cannot be modified. However, accessing properties from frozen objects may exhibit a similar performance profile to sealed objects, likely with a lower execution rate due to additional constraints on mutability.Overall, this benchmark helps to understand the performance implications of using sealed objects versus ordinary objects in JavaScript, providing insights into when each approach might be more advantageous based on specific coding needs and project requirements.