Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array of key values - Object.entries VS Object.keys
(version: 0)
Comparing performance of:
Object.entries vs Object.keys
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()] = makeid(); }
Tests:
Object.entries
Object.entries(window.parentObj).map(([id, name]) => { return {id, name} });
Object.keys
Object.keys(window.parentObj).map(id => { const name = window.parentObj[id]; return { id, name }; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.entries
Object.keys
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 explain what is being tested. **Benchmark Overview** The benchmark compares two approaches to iterate over an object in JavaScript: `Object.entries` and `Object.keys`. The test case creates an object with 100 keys (unique identifiers) and values, generated randomly using a custom function `makeid()`. The goal is to determine which approach is faster for this specific use case. **Options Compared** Two options are being compared: 1. **`Object.entries()`**: This method returns an array of key-value pairs in the object, where each pair is an array with two elements: the key and the value. 2. **`Object.keys()`**: This method returns an array of strings representing the keys of the object. **Pros and Cons** Here's a brief overview of each approach: * `Object.entries()`: + Pros: - Directly returns an array of key-value pairs, making it easier to process. - Can be faster when working with objects where you need both key and value. + Cons: - Returns an array of arrays (each element is an array), which can lead to unnecessary overhead. * `Object.keys()`: + Pros: - Returns a simple array of strings, making it easier to iterate over the keys. - Less memory-intensive than `Object.entries()`. + Cons: - Requires additional processing to access the corresponding values. **Library and Purpose** In this benchmark, no libraries are explicitly used. However, `window.parentObj` is a custom object created in the script preparation code. Its purpose is to serve as a test subject for iterating over its key-value pairs using both `Object.entries()` and `Object.keys()` methods. **Special JS Feature or Syntax (None)** There are no special JavaScript features or syntax used in this benchmark. **Alternative Approaches** If you need to iterate over an object's keys, other approaches might include: * Using a for...in loop: `for (var key in obj) { ... }` * Using a for...of loop with Object iterators: `for (const [key, value] of Object.entries(obj)) { ... }` (not used in this benchmark) * Using a library like Lodash's `keys()` function: `_object.keys()` Keep in mind that the choice of approach depends on your specific use case and performance requirements. **Benchmark Preparation Code Explanation** The script preparation code creates an object with 100 random keys and values, assigns it to `window.parentObj`, and then iterates over its key-value pairs using both `Object.entries()` and `Object.keys()` methods. The resulting benchmark data is stored in the `RawUAString` property of each test case. **Latest Benchmark Result Explanation** The latest benchmark result shows the execution per second for each browser (in this case, only Firefox 102) on a Linux desktop system. The values indicate that `Object.entries()` outperforms `Object.keys()`, with approximately 2.5 times more executions per second.
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 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.entries VS Object.keys VS Object.keys with extra array VS Object.entries without array VS Object.keys as separate array with for loop
Comments
Confirm delete:
Do you really want to delete benchmark?