Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Destructure from Object.seal vs Object.freeze vs normal(special edition)
(version: 0)
Comparing performance of:
seal vs normal vs freeze
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = () => { return Object.seal({ x: 321, y: 123 }); }; var b = () => { return { x: 321, y: 123 }; } var c = () => { return Object.freeze({ x: 321, y: 123}); };
Tests:
seal
const {x,y} = a()
normal
const {x,y} = b()
freeze
const {x,y} = c()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
seal
normal
freeze
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. The benchmark is comparing three different approaches to object creation: 1. `Object.seal()`: This method seals an object, making it immutable by preventing new properties from being added or existing properties from being deleted. However, this does not prevent property assignment (e.g., `obj.x = 321;`) if the object was previously frozen or sealed. 2. `Object.freeze()`: This method freezes an object, making it immutable by preventing any modifications to its properties. Frozen objects cannot be modified even by their owner, and attempting to do so will result in a TypeError. 3. `Normal (special edition)` (likely referring to the default way of creating an object): In JavaScript, when you create an object using the `{}` syntax or the `new Object()` constructor, it is not immutable by default. Now, let's discuss the pros and cons of each approach: * `Object.seal()`: This method provides a balance between immutability and mutable behavior. It prevents new properties from being added or existing ones deleted but still allows property assignment. + Pros: More flexible than `Object.freeze()` since it doesn't prevent property assignment, making it suitable for scenarios where you need to modify the object's properties. + Cons: Not as restrictive as `Object.freeze()`, which ensures that an object cannot be modified at all. * `Object.freeze()`: This method provides maximum immutability but can lead to performance issues due to the added overhead of freezing an object. Additionally, frozen objects are not enumerable in some contexts, such as in for...of loops or Array.prototype.forEach(). + Pros: Ensures that an object cannot be modified at all, making it suitable for scenarios where immutability is critical. + Cons: Can introduce performance overhead and limitations when working with frozen objects. * `Normal (special edition)` (default object creation): This method does not provide any guarantees about the object's mutability. It allows property assignment, which can be both beneficial and detrimental depending on the context. + Pros: No added overhead or restrictions, making it suitable for most use cases where immutability is not strictly necessary. + Cons: Does not ensure immutability, which might lead to unintended modifications of the object. The test cases are designed to measure the performance difference between these approaches. The benchmark script preparation code demonstrates each approach by creating an object and then destructuring it into two variables (`x` and `y`). The results show that: * `Object.seal()` is faster than `Object.freeze()`, as it requires less overhead for property assignment. * `Normal (special edition)` is the fastest, as it does not impose any additional restrictions or overhead. Other alternatives to consider in this context are: * Using `Object.create(null)` instead of `{}` when creating an object. This approach allows for more control over the object's prototype chain and properties but may have performance implications. * Utilizing modern JavaScript features like classes, which can provide a more explicit and type-safe way of creating objects with immutability guarantees. Keep in mind that these alternatives are not directly related to the main benchmarking comparison between `Object.seal()`, `Object.freeze()`, and normal object creation.
Related benchmarks:
Destructure from Object.seal vs Object.freeze vs normal
Object.freeze vs Object.seal vs Native
Access from Object.seal vs Object.freeze vs normal
normal vs Object.freeze vs Object.seal performance
Comments
Confirm delete:
Do you really want to delete benchmark?