Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object values vs Object.entries VS Object.keys
(version: 0)
Comparing performance of:
Object.entries vs Object.keys vs Object.values
Created:
5 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()] = { innerVal: makeid() }; }
Tests:
Object.entries
const newObj = {}; Object.entries(window.parentObj).forEach(([k, v], i) => { if ((i % 2) === 0) { newObj[i] = v; } });
Object.keys
const newObj = {}; Object.keys(window.parentObj).forEach((k, i) => { if ((i % 2) === 0) { newObj[i] = window.parentObj[k]; } });
Object.values
const newObj = {}; Object.values(window.parentObj).forEach((v, i) => { if ((i % 2) === 0) { newObj[i] = v; } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.entries
Object.keys
Object.values
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.entries
105452.0 Ops/sec
Object.keys
427093.1 Ops/sec
Object.values
134935.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **Benchmark Overview** The benchmark compares the performance of three different ways to access object values in JavaScript: `Object.entries`, `Object.keys`, and `Object.values`. The test case generates a large number of objects with random keys, where each key is a string generated by concatenating 5 random characters from an alphabet. **Options Compared** 1. **`Object.entries`**: Returns an array of key-value pairs in the object, where the first element of the pair is the key and the second element is the value. 2. **`Object.keys()`**: Returns an array of strings representing the keys of the object. 3. **`Object.values()`**: Returns an array of values in the object. **Pros and Cons** * **`Object.entries`**: + Pros: Can be used to access both keys and values in a single operation, which can lead to better cache locality. + Cons: Requires additional iteration over the key-value pairs, which can be slower for small objects or objects with many duplicate keys. * **`Object.keys()`**: + Pros: Faster for small objects or objects with few unique keys, as it only requires iterating over the keys. + Cons: Does not provide direct access to values, and using `newObj[k] = window.parentObj[k];` can lead to slower performance due to repeated lookups. * **`Object.values()`**: + Pros: Similar to `Object.entries`, as it returns an array of values without requiring additional iteration over keys. + Cons: Similar to `Object.keys()`, as it does not provide direct access to keys. **Library and Special Features** None of the options used a specific library, but they did use the `window.parentObj` object, which is a global variable that allows the test case to generate and access objects from outside the JavaScript context. This suggests that the benchmark is designed to measure performance in a specific environment or context. **Other Considerations** * The benchmark generates a large number of objects with random keys, which can lead to high cache thrashing and make it difficult to accurately compare performance. * The test case uses a simple iteration over the key-value pairs, without using any optimizations like `for...of` loops or `Array.prototype.forEach()`. * The benchmark only measures performance on a single browser version (Firefox 85), which may not be representative of other browsers or versions. **Alternatives** Other alternatives to measure object value access performance might include: * Using `for...in` loops instead of `Object.keys()` and `Object.values()` * Measuring performance using different data structures, such as arrays or maps * Adding additional operations to the test case, such as modifying or deleting objects during iteration * Using a larger number of iterations or more complex object hierarchies
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 entry counting: Object.entries VS Object.keys VS Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?