Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Yet Another Object.keys vs Object.values
(version: 0)
Comparing performance of:
for-in vs Object.keys vs Object.values
Created:
one year ago
by:
Guest
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 }, };
Tests:
for-in
let sum = 0; for (let key in obj) sum += obj[key].num;
Object.keys
const sum = Object.keys(obj).reduce((acc, k) => acc + obj[k].num, 0);
Object.values
const sum = Object.values(obj).reduce((acc, v) => acc + v.num, 0);
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for-in
3608013.0 Ops/sec
Object.keys
2757881.0 Ops/sec
Object.values
8605110.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition:** The benchmark measures the performance of three different approaches to iterate over an object: 1. **for-in**: Using the `in` operator with an array, which iterates over the keys of the object. 2. **Object.keys**: Using the `Object.keys()` method to get an array of the object's keys, and then using the `reduce()` method to iterate over them. 3. **Object.values**: Using the `Object.values()` method to get an array of the object's values, and then using the `reduce()` method to iterate over them. **Pros and Cons of Each Approach:** 1. **for-in**: * Pros: * Simple and concise syntax * Fast execution (as it uses the engine's built-in iteration mechanism) * Cons: * May not work as expected if the object is not a plain object * May have issues with prototype chains 2. **Object.keys**: * Pros: * Standardized and documented API * Works well with objects that are not plain (as it returns an array of keys) * Can be used in more complex scenarios where iteration is required * Cons: * May have slower execution compared to `for-in` due to the overhead of creating and iterating over an array 3. **Object.values**: * Pros: * Standardized and documented API * Works well with objects that are not plain (as it returns an array of values) * Can be used in more complex scenarios where iteration is required * Cons: * May have slower execution compared to `for-in` due to the overhead of creating and iterating over an array **Library Usage:** None of the benchmark cases use a specific library. **Special JS Features or Syntax:** There are no special JavaScript features or syntax mentioned in the benchmark definition. However, it's worth noting that `Object.keys()` and `Object.values()` were introduced in ECMAScript 2015 (ES6) as part of the standardization process. **Other Alternatives:** Some other alternatives for iterating over objects include: 1. **for...in**: This is the traditional way to iterate over an object's properties, but it has some limitations and potential issues. 2. **Object.entries()**: This method returns an array of a given object's key-value pairs, which can be used with `reduce()` or other iteration methods. 3. **Array.prototype.forEach()**: This method allows iterating over an array of object keys or values using a callback function. In general, the choice of iteration approach depends on the specific use case and requirements. For example: * If you need to iterate over an object's keys, `Object.keys()` might be a good choice. * If you need to iterate over an object's values, `Object.values()` might be a better option. * If you have a complex scenario that requires iteration, using `reduce()` with `Object.keys()` or `Object.entries()` might be the way to go.
Related benchmarks:
Object speard vs assign
Object.keys vs Object.values
object destruction vs. dot notation 2
key in object vs object.key
Object.keys vs Object.entries vs Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?