Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for-in vs object.keys vs object.values for objects: searching
(version: 7)
Comparing performance of:
for-in vs Object.keys vs Object.values
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = { 'a': { id: 'a', num: 1 }, 'b': { id: 'b', num: 1 }, 'c': { id: 'c', num: 1 }, 'd': { id: 'd', num: 1 }, 'e': { id: 'e', num: 1 }, 'f': { id: 'f', num: 1 }, 'g': { id: 'g', num: 1 }, }; var ids = Object.values(obj).map(x => x.id); var n = 1;
Tests:
for-in
for (var i=n; i > 0; i--) { for (const find of ids) { for (var key in obj) { if (obj[key].id === find) { console.log(obj[key].id); break; } } } }
Object.keys
for (var i=n; i > 0; i--) { for (const find of ids) { for (var key of Object.keys(obj)) { if (obj[key].id === find) { console.log(obj[key].id); break; } } } }
Object.values
for (var i=n; i > 0; i--) { for (const find of ids) { for (var o of Object.values(obj)) { if (o.id === find) { console.log(o.id); break; } } } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for-in
Object.keys
Object.values
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 dive into the world of JavaScript microbenchmarks. **Benchmark Definition** The benchmark compares three ways to search for an ID in an object: `for-in`, `Object.keys`, and `Object.values`. The test case uses a nested object with multiple properties, where each property has an `id` field. The objective is to find the first occurrence of an `id` value in the object. **Script Preparation Code** The script prepares the benchmark by creating a large object with 10 properties, each with an `id` field. It then extracts all the IDs from the object using `Object.values`, which are stored in the `ids` variable. **Html Preparation Code** There is no HTML preparation code provided. **Benchmark Options Compared** The three options compared are: 1. **for-in**: This method uses a traditional `for` loop to iterate over the properties of the object, and then checks each property's value against the target ID. 2. **Object.keys**: This method returns an array of property names of the object, which can be iterated over using a `for` loop or other iteration methods. The code uses `Object.keys(obj)` to get the property names, and then checks each name's corresponding value against the target ID. 3. **Object.values**: This method returns an array of values of the object's properties, which can be iterated over using a `for` loop or other iteration methods. The code uses `Object.values(obj)` to get the values, and then checks each value against the target ID. **Pros and Cons** * **for-in**: + Pros: Can handle nested objects and has good support for iterating over properties. + Cons: May be slower due to its traditional loop implementation and potential issues with object property iteration order. * **Object.keys**: + Pros: Efficiently returns an array of property names, making it suitable for iteration. + Cons: Requires additional memory allocation for the array, which might not be significant but still a consideration. * **Object.values**: + Pros: Returns an array of values directly, avoiding unnecessary iterations over property names. + Cons: May require more memory to store the array of values, especially for large objects. **Library and Purpose** In this benchmark, `Object.keys()` and `Object.values()` are built-in JavaScript methods that provide efficient ways to iterate over object properties. They do not rely on any external libraries. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax used in this benchmark. However, it's worth noting that the use of `const` (not shown in the code snippet) and arrow functions (`=>`) are modern JavaScript features introduced in ECMAScript 2015 (ES6). **Alternatives** Other alternatives to explore when searching for values in an object include: 1. **forEach()**: A method provided by Array-like objects, including arrays of property names or values, which can be used to iterate over the elements. 2. **for...in** with `hasOwnProperty()` (not shown here): While not as efficient as `Object.keys()` and `Object.values()`, this approach provides a way to iterate over properties without relying on array-based iteration methods. 3. **Array.prototype.map()**: Can be used in combination with `Object.keys()` or `Object.values()` to create an array of filtered values. These alternatives may offer different performance characteristics, trade-offs between memory usage and iteration efficiency, or more concise code structures depending on the specific use case.
Related benchmarks:
For in vs For of
object.keys map vs for in
entries vs keys lookup
checks if object has any key - Object.keys vs for key in 2
Object.entries VS Object.keys with obj[key]
Comments
Confirm delete:
Do you really want to delete benchmark?