Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values(obj) vs for...in
(version: 0)
Object.values(obj) function vs using a for...in loop.
Comparing performance of:
Object.values(obj) vs for...in
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function extractValues(obj) { const result = []; for(var key in obj) { result.push(obj[key]); } return result; }
Tests:
Object.values(obj)
var a = { a: 1, b: 2, c: 3}; Object.values(a);
for...in
var a = { a: 1, b: 2, c: 3}; extractValues(a);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.values(obj)
for...in
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
gemma2:9b
, generated one year ago):
This benchmark compares two methods for extracting values from an object in JavaScript: `Object.values()` and a traditional `for...in` loop. **Options Compared:** * **`Object.values(obj)`**: This is a built-in JavaScript method that directly returns an array containing all the values of an object. It's concise and efficient. * **`for...in` loop**: This is a classic way to iterate over the properties of an object in JavaScript. It involves manually pushing each value from the object into an array. **Pros/Cons:** | Method | Pros | Cons | |-----------------|-------------------------------------------|----------------------------------------------| | `Object.values()` | * **Concise and readable:** One line of code does the job.<br>* **Potentially faster:** Often optimized by the JavaScript engine. | * **Less versatile:** Doesn't allow you to access property names along with values. | | `for...in` loop | * **More control:** You can access both property names and values during iteration. | * **Verbose:** Requires more lines of code.<br>* **Potential for performance issues:** Less optimized than built-in methods in some cases. **Other Considerations:** * **Object Size:** For very large objects, the difference in performance between these methods might become more noticeable. * **Use Case:** If you only need the values and don't require property names, `Object.values()` is the clear winner. If you need both values and names, `for...in` provides that flexibility. **Alternatives:** * **`Object.entries(obj)`**: This method returns an array of key-value pairs from the object. It offers a balance between conciseness and control. Let me know if you have any other questions about this benchmark or JavaScript benchmarking in general!
Related benchmarks:
Performance of Object.values(obj) vs_.values() vs for...in to extract values from an object
Performance of Object.values(obj) vs_.values() vs for-in to extract values from an object
Performance of Object.values(obj) vs_.values() vs for...in to extract values from an object with 1000 entries-2
Performance of Object.values(obj) vs_.values() vs for...in to extract values from an object with 1000 entries-3
Comments
Confirm delete:
Do you really want to delete benchmark?