Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values vs For..In
(version: 1)
DIferencias
Comparing performance of:
For..In vs Object.values
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const schedules = { "acb4a514-1785-470a-9713-037144575165": { "id": "acb4a514-1785-470a-9713-037144575165", "nombre": "Schedule 1", "inicio": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 8 }, "fin": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 20 }, "duracion": 0, "resource": "", "progreso": 0.13, "dependencia": "" }, "acb4a514-1785-470a-9713-037144575166": { "id": "acb4a514-1785-470a-9713-037144575165", "nombre": "Schedule 1", "inicio": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 8 }, "fin": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 20 }, "duracion": 0, "resource": "", "progreso": 0.13, "dependencia": "" }, "acb4a514-1785-470a-9713-037144575167": { "id": "acb4a514-1785-470a-9713-037144575165", "nombre": "Schedule 1", "inicio": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 8 }, "fin": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 20 }, "duracion": 0, "resource": "", "progreso": 0.13, "dependencia": "" }, "acb4a514-1785-470a-9713-037144575168": { "id": "acb4a514-1785-470a-9713-037144575165", "nombre": "Schedule 1", "inicio": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 8 }, "fin": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 20 }, "duracion": 0, "resource": "", "progreso": 0.13, "dependencia": "" }, "acb4a514-1785-470a-9713-037144575169": { "id": "acb4a514-1785-470a-9713-037144575165", "nombre": "Schedule 1", "inicio": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 8 }, "fin": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 20 }, "duracion": 0, "resource": "", "progreso": 0.13, "dependencia": "" }, "acb4a514-1785-470a-9713-037144575170": { "id": "acb4a514-1785-470a-9713-037144575165", "nombre": "Schedule 1", "inicio": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 8 }, "fin": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 20 }, "duracion": 0, "resource": "", "progreso": 0.13, "dependencia": "" }, "acb4a514-1785-470a-9713-037144575171": { "id": "acb4a514-1785-470a-9713-037144575165", "nombre": "Schedule 1", "inicio": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 8 }, "fin": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 20 }, "duracion": 0, "resource": "", "progreso": 0.13, "dependencia": "" }, "acb4a514-1785-470a-9713-037144575172": { "id": "acb4a514-1785-470a-9713-037144575165", "nombre": "Schedule 1", "inicio": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 8 }, "fin": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 20 }, "duracion": 0, "resource": "", "progreso": 0.13, "dependencia": "" }, "acb4a514-1785-470a-9713-037144575173": { "id": "acb4a514-1785-470a-9713-037144575165", "nombre": "Schedule 1", "inicio": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 8 }, "fin": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 20 }, "duracion": 0, "resource": "", "progreso": 0.13, "dependencia": "" }, "acb4a514-1785-470a-9713-037144575174": { "id": "acb4a514-1785-470a-9713-037144575165", "nombre": "Schedule 1", "inicio": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 8 }, "fin": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 20 }, "duracion": 0, "resource": "", "progreso": 0.13, "dependencia": "" }, "acb4a514-1785-470a-9713-037144575175": { "id": "acb4a514-1785-470a-9713-037144575165", "nombre": "Schedule 1", "inicio": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 8 }, "fin": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 20 }, "duracion": 0, "resource": "", "progreso": 0.13, "dependencia": "" }, "acb4a514-1785-470a-9713-037144575176": { "id": "acb4a514-1785-470a-9713-037144575165", "nombre": "Schedule 1", "inicio": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 8 }, "fin": { "calendar": { "identifier": "gregory" }, "era": "AD", "year": 2024, "month": 11, "day": 20 }, "duracion": 0, "resource": "", "progreso": 0.13, "dependencia": "" } }
Tests:
For..In
for (var i=10000; i > 0; i--) { const schedulesArray = []; for (const key in schedules) { if (schedules.hasOwnProperty(key)) { schedulesArray.push(schedules[key]); } } }
Object.values
for (var i=10000; i > 0; i--) { Object.values(schedules) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
For..In
Object.values
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0
Browser/OS:
Firefox 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For..In
586.9 Ops/sec
Object.values
747.5 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the JSON provided measures the performance difference between two approaches for iterating over the keys of an object in JavaScript: using the `for..in` loop and using the `Object.values()` method. The benchmark is specifically focused on performance, measuring how many executions can be performed per second for each method. ### Benchmark Options Compared: 1. **For..In Loop**: - **Test Case**: ```javascript for (var i=10000; i > 0; i--) { const schedulesArray = []; for (const key in schedules) { if (schedules.hasOwnProperty(key)) { schedulesArray.push(schedules[key]); } } } ``` - **Description**: This method iterates over the keys of the `schedules` object using the `for..in` loop. The `if` check ensures that only the object's own properties (not inherited ones) are considered, which is crucial for accurately retrieving data from the object without unwanted entries. - **Pros**: - Works on any object that is enumerable and iterates over all properties, including inherited ones (when `hasOwnProperty` is not used). - Familiar syntax for developers accustomed to traditional loop mechanisms. - **Cons**: - Can be slower because of the overhead of checking `hasOwnProperty` for each iteration. - Requires additional logic to filter inherited properties, making it less clean in scenarios specifically requiring own properties. 2. **Object.values() Method**: - **Test Case**: ```javascript for (var i=10000; i > 0; i--) { Object.values(schedules); } ``` - **Description**: This method uses `Object.values()` to directly retrieve an array of the values of the `schedules` object. It does not require any loops or conditional checks to filter properties. - **Pros**: - Provides cleaner and more readable code. - Generally faster as the method is optimized internally by JavaScript engines to retrieve values directly. - **Cons**: - It only works on enumerable own properties and does not return inherited properties (but this behavior is usually desired). ### Performance Results: The results of the benchmark show a significant performance difference between the two methods: - **Object.values**: 321.67 executions per second. - **For..In**: 29.49 executions per second. This indicates that the `Object.values()` method is considerably faster than the `for..in` loop iteration approach for the specific case tested. ### Conclusion and Alternatives: This benchmark shows a good use case for comparing syntactic approaches in JavaScript. Understanding the performance implications of using native methods (like `Object.values()`) compared to traditional looping constructs (like `for..in`) is essential for writing efficient JavaScript code. ### Alternatives: - **Using `Object.keys()`**: Similar to `Object.values()`, this method returns an array of an object's own property names, which can then be iterated over. - **`forEach()`**: If you convert the object values into an array using `Object.values()`, you can further process them using the array's `forEach()` method. - **Using `for..of` with `Object.values()`**: This combination is also very readable and allows for concise code when you need to process each value of the object. Selecting between these alternatives often depends on the specific requirements for property access (e.g., needing to access keys vs. values) and the performance context of the application.
Related benchmarks:
test 4bc5a6e8-e19c-46c7-8c39-c70b7152fe5f
Nested vs Cascade
Different ways of creating objects 2
json and
me vs spenny
array iteration vs _.each vs map vs for..of vs for loop AP
array iteration vs _.each vs map vs for..of vs for loop AP1
Jason stringify vs object.keys
Shiogui - Push x Reduce Logged
Comments
Confirm delete:
Do you really want to delete benchmark?