Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries VS Object.keys VS Object.values
(version: 0)
Comparing performance of:
Object.entries vs Object.keys vs Object.values vs Object.entries2
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function makeid() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (var i = 0; i < 5; i++) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } window.parentObj = {}; for (let i = 0; i < 100; i++) { window.parentObj[makeid()] = makeid(); }
Tests:
Object.entries
const newObj = {}; Object.entries(window.parentObj).forEach(([k, v], i) => { if ((i % 2) === 0) { newObj[k] = v; } });
Object.keys
const newObj = {}; Object.keys(window.parentObj).forEach((k, i) => { if ((i % 2) === 0) { newObj[k] = window.parentObj[k]; } });
Object.values
const newObj = {}; Object.values(window.parentObj).forEach((v, i) => { if ((i % 2) === 0) { newObj[i] = window.parentObj[v]; } });
Object.entries2
const newObj = {}; Object.entries(window.parentObj).forEach(([k, v], i) => { if ((i % 2) === 0) { newObj[i] = v; } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Object.entries
Object.keys
Object.values
Object.entries2
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 break down the provided benchmark and its test cases. **Benchmark Purpose** The benchmark is designed to compare the performance of three different methods for accessing and manipulating object properties in JavaScript: `Object.entries()`, `Object.keys()`, and `Object.values()`. These methods are used to iterate over an object's key-value pairs. However, when using these methods, you need to decide how to handle the case where the object is not iterable (e.g., a non-object value). In this benchmark, the test cases aim to compare different approaches for handling such scenarios. **Comparison Options** There are three main comparison options: 1. **`Object.entries()`**: This method returns an array of a given object's own enumerable string-keyed property [key, value] pairs. The code uses `forEach()` with `[k, v], i` to iterate over the pairs and access specific values. 2. **`Object.keys()`**: This method returns an array of all the accessible keys for an object. It is used in this benchmark with `(k, i)` to iterate over the key-value pairs where `i` is an index. 3. **`Object.values()`**: Similar to `Object.entries()`, but it only returns the values instead of both keys and values. In the code snippet, it's used with `(v, i)` as the iteration parameters. **Pros and Cons** * *Consistency*: All three methods are designed to work with iterable objects, which can lead to inconsistent performance in non-iterable cases. * Using `Object.entries()` or `Object.values()` works well when the object is iterable because it avoids the overhead of parsing keys. However, when dealing with non-iterable values, this method will produce errors. * The `Object.keys()` approach has some limitations as well; if the object doesn't have any numeric key, it won't include that key in its result set, and may cause performance issues for large objects. **Pros of `Object.entries()`**: * It includes all properties in both keys and values. * If the value is not iterable, you can just ignore it as with Object.keys().
Related benchmarks:
Object values: Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS Object values: Object.entries loop for
Object.key vs Object.value vs Object.entries
Array of key values - Object.entries VS Object.keys
Object entry counting: Object.entries VS Object.keys VS Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?