Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.values vs for
(version: 1)
Comparing performance of:
Object.values vs for
Created:
4 years ago
by:
Registered User
Jump to the latest result
Tests:
Object.values
const o = [...Array(100000).keys()].reduce((r, i) => { r[i] = i; return r; }, {}); const a = Object.values(o).map(v => v + 1);
for
const o = [...Array(100000).keys()].reduce((r, i) => { r[i] = i; return r; }, {}); const a = []; for (p in o) { a.push(o[p] + 1); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.values
for
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):
I'd be happy to explain the benchmark and its options. **Benchmark Description** The benchmark tests two ways of accessing an object's property values: `Object.values` and a traditional loop using `for...in`. The test case creates an object `o` by reducing an array of 100,000 keys using the `reduce()` method. The resulting object is then used to calculate the square of each value using either `Object.values` or a traditional loop. **Options Compared** The benchmark compares two options: 1. **`Object.values`**: This method returns an array containing all the property values of an object. 2. **Traditional Loop with `for...in`**: This approach uses a loop to iterate over the object's properties and calculates the square of each value. **Pros and Cons** **`Object.values`**: Pros: * Concise and expressive syntax * Fast execution, as it avoids the overhead of iterating over the object's properties Cons: * May not be suitable for very large objects or complex objects with multiple inheritance paths (due to its use of `Object.prototype.toString()` under the hood) **Traditional Loop with `for...in`**: Pros: * Can be more intuitive and easy to understand, especially for developers familiar with traditional loop patterns * Does not have the same performance overhead as `Object.values` Cons: * More verbose syntax * May be slower due to the iteration overhead **Other Considerations** In general, when working with objects in JavaScript, it's a good idea to consider using `Object.entries()` or `for...of` loops instead of `Object.keys()` or `for...in`. These newer methods offer improved performance and are more concise. **Library and Special JS Feature** Neither of the benchmark test cases uses any libraries. However, it does utilize JavaScript's modern syntax features, such as template literals (`\r\n`) in the `reduce` method. **Other Alternatives** If you're looking for alternative ways to access an object's property values, consider using: * `Object.entries()`: Returns an array of a given object's own enumerable string-keyed property [key, value] pairs. * `for...of` loops: Iterate over an object's properties using a loop that iterates over the property names. Here is an example of how you could rewrite the benchmark test case using these alternatives: ```javascript const o = [...Array(100000).keys()].reduce((r, i) => { r[i] = i; return r; }, {}); // Using Object.entries() for (const [key, value] of Object.entries(o)) { console.log(key + ": " + (value + 1)); } // Using for...of loops for (const key in o) { console.log(key + ": " + (o[key] + 1)); } ``` These alternatives offer improved performance and are more concise than the traditional loop approach.
Related benchmarks:
Object entries vs forin
Object.keys/Object.values/Object.entries
for-in vs object.keys vs object.values for objects perf
Object.keys vs Object.entries vs Object.values
Object.entries vs Object.values basic
Comments
Confirm delete:
Do you really want to delete benchmark?