Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries VS Object.values
(version: 0)
Comparing performance of:
Object.entries vs Object.values
Created:
4 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 uniqNodes = new Set(); Object.entries(window.parentObj).forEach(([_identifier, node]) => { if (!uniqNodes.has(node)) { uniqNodes.add(node); } });
Object.values
const uniqNodes = new Set(); Object.values(window.parentObj).forEach((node) => { if (!uniqNodes.has(node)) { uniqNodes.add(node); } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.entries
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 break down the provided benchmarking setup and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of two approaches: `Object.entries` and `Object.values`. Both methods are used to iterate over an object's key-value pairs. The benchmark tests which approach is faster, using a synthetic dataset created by generating 100 objects with random properties. **Options Compared** Two options are compared: 1. **`Object.entries()`**: This method returns an array of arrays, where each inner array contains the key-value pair for that particular iteration. 2. **`Object.values()`**: This method returns an array containing all the values of the object, without their corresponding keys. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * `Object.entries()`: + Pros: Can be used to access both key and value in a single iteration, which might be useful in certain scenarios. + Cons: Returns an array of arrays, which can lead to slower performance due to the extra indirection. In this benchmark, it's also worth noting that `Object.entries()` requires creating an array to store the results, whereas `Object.values()` doesn't require any additional storage. * `Object.values()`: + Pros: Returns a flat array of values without the need for additional storage or complex iteration logic. + Cons: Forces the use of a separate variable to store the result, which might be seen as less readable or less flexible compared to `Object.entries()`. **Library and Purpose** In this benchmark, a library called "Set" is used in both test cases. A Set is an unordered collection of unique values, implemented as a hash table. The purpose of using a Set here is likely to check for uniqueness among the nodes being processed. In the first test case (`Object.entries`), the Set is used to keep track of unique nodes encountered during iteration, while in the second test case (`Object.values`), it's used similarly to ensure that each node is only added once. **Special JS Feature or Syntax** There are no special JavaScript features or syntax being tested in this benchmark. The code uses standard ECMAScript language constructs and doesn't include any experimental or proprietary APIs. **Alternative Approaches** Other alternatives for iterating over an object's key-value pairs could be: 1. **For...in**: This is a traditional loop that iterates over the property names of an object. 2. **for...of**: Similar to `For...in`, but designed specifically for iterating over arrays and other iterable objects. 3. **Array.prototype.forEach()`: A modern, concise way to iterate over an array or any other iterable. However, `Object.entries()` and `Object.values()` are the most efficient and widely supported approaches in modern JavaScript, especially when working with large datasets like this benchmark. Keep in mind that performance differences between these methods can be significant, depending on the specific use case and dataset size.
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
Object values: Object.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS for .. of
Object.entries VS Object.keys VS Object.values
Object entry counting: Object.entries VS Object.keys VS Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?