Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Looping Frozen object: Object Freeze vs Without Object Freeze
(version: 1)
Comparing performance of:
Loop Frozen Object vs Loop Non-Frozen Object
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const birdSpecies = { forest: { species: [ { name: "Northern Cardinal", physicalCharacteristics: { color: "Red", size: "Medium", beakType: "Conical" }, diet: { primary: "Seeds", secondary: "Fruits" }, habitat: "Woodlands and forests" }, { name: "Woodpecker", physicalCharacteristics: { color: "Black, White, and Red", size: "Medium", beakType: "Chisel-shaped" }, diet: { primary: "Insects", secondary: "Tree sap" }, habitat: "Wooded areas" } ] }, desert: { species: [ { name: "Roadrunner", physicalCharacteristics: { color: "Brown and White", size: "Medium", beakType: "Long and Slim" }, diet: { primary: "Insects", secondary: "Small reptiles" }, habitat: "Desert and arid regions" }, { name: "Cactus Wren", physicalCharacteristics: { color: "Brown with streaks", size: "Small", beakType: "Curved" }, diet: { primary: "Insects", secondary: "Seeds" }, habitat: "Cacti and desert shrubs" } ] }, wetlands: { species: [ { name: "Great Blue Heron", physicalCharacteristics: { color: "Blue-gray", size: "Large", beakType: "Long and Sharp" }, diet: { primary: "Fish", secondary: "Amphibians" }, habitat: "Wetlands and marshes" }, { name: "American Coot", physicalCharacteristics: { color: "Dark gray with white bill", size: "Medium", beakType: "Flat and short" }, diet: { primary: "Aquatic plants", secondary: "Small invertebrates" }, habitat: "Lakes and wetlands" } ] } } const frozenObject = Object.freeze(birdSpecies) const nonFrozenObject = birdSpecies
Tests:
Loop Frozen Object
for (const [habitat, data] of Object.entries(frozenObject)) { console.log(`Habitat: ${habitat}`) for (const bird of data.species) { console.log(` Name: ${bird.name}`) console.log(` Physical Characteristics:`) for (const [key, value] of Object.entries(bird.physicalCharacteristics)) { console.log(` ${key}: ${value}`) } console.log(` Diet:`) for (const [key, value] of Object.entries(bird.diet)) { console.log(` ${key}: ${value}`) } console.log(` Habitat: ${bird.habitat}`) } }
Loop Non-Frozen Object
for (const [habitat, data] of Object.entries(nonFrozenObject)) { console.log(`Habitat: ${habitat}`) for (const bird of data.species) { console.log(` Name: ${bird.name}`) console.log(` Physical Characteristics:`) for (const [key, value] of Object.entries(bird.physicalCharacteristics)) { console.log(` ${key}: ${value}`) } console.log(` Diet:`) for (const [key, value] of Object.entries(bird.diet)) { console.log(` ${key}: ${value}`) } console.log(` Habitat: ${bird.habitat}`) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Loop Frozen Object
Loop Non-Frozen Object
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Loop Frozen Object
8822.0 Ops/sec
Loop Non-Frozen Object
8800.9 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark focuses on measuring performance differences when looping through properties of a JavaScript object that is either frozen or non-frozen. Here’s a breakdown of what is being tested, the comparisons made, and the implications of the results. ### Objects in the Benchmark 1. **Frozen Object (`frozenObject`)**: - Created using JavaScript's `Object.freeze()`, which prevents the modification of the object's properties, effectively making it immutable. Once an object is frozen, you cannot add, delete, or change its properties. 2. **Non-Frozen Object (`nonFrozenObject`)**: - Simply refers to the same `birdSpecies` object without any restrictions. This object can be modified at any time. ### Performance Comparison Two test cases are run: 1. **Loop Frozen Object**: This test benchmarks how quickly the code can iterate over the properties of the frozen object. 2. **Loop Non-Frozen Object**: This test measures the iteration speed for a non-frozen object. In both test cases, the same nested looping construct is used to access and log various properties of the bird species across multiple habitats. ### Results Summary - **Frozen Object Execution**: 2166.06 executions per second - **Non-Frozen Object Execution**: 2121.12 executions per second ### Pros and Cons of Frozen vs. Non-Frozen Objects - **Frozen Object**: - **Pros**: - Enhances performance in specific cases since the engine can optimize access patterns due to the immutability of the structure. - Improves maintainability by preventing accidental modifications, thus aiding in more predictable behaviors. - **Cons**: - Does not allow for property changes, which can be a drawback in scenarios where dynamic updates are necessary. - **Non-Frozen Object**: - **Pros**: - Flexibility in data manipulation (adding, deleting, or updating properties). - **Cons**: - Potential overhead from tracking changes, which can lead to fewer optimizations by the JavaScript engine, possibly resulting in lower performance in certain contexts. ### Other Considerations - **Memory Usage**: Frozen objects can have advantages regarding memory usage since shared references can be made without the risk of alteration. Non-frozen objects may require additional memory for copies if frequent modifications are made. - **Use Cases**: Choosing between frozen and non-frozen depends on the use case. If the data structure is constant and does not require updates, using frozen objects can be beneficial. ### Alternatives - Instead of freezing objects, developers can also use techniques such as: - **Object.create(null)** to create an object without prototype properties, reducing overhead in certain contexts. - **Immutability Helpers**: Libraries like Immutable.js that provide structures optimized for immutability while offering straightforward methods for accessing and modifying data. - Data structures like **Typed Arrays** or other specialized containers may also be suitable alternatives depending on the specific needs around data handling, memory constraints, or performance optimizations required. In conclusion, this benchmark provides insight into the performance implications of using frozen versus non-frozen JavaScript objects, illuminating important considerations in application architecture and coding practices.
Related benchmarks:
for each for of christian
For Loop vs While Loop vs indexOf
AOC Day 7
Filter vs slice - neokore
shift and push vs slice
Group by
inherit vs copy
Searching in an array of objects
Looping Frozen object: Object Freeze vs Deep Freeze vs Without Object Freeze vs
Comments
Confirm delete:
Do you really want to delete benchmark?