Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.entries VS Object.keys.map
(version: 1)
Comparing performance of:
Object.entries vs Object.keys.map
Created:
4 years ago
by:
Registered User
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.map
const newObj = {}; Object.keys(window.parentObj).map(key => [key, window.parentObj[key]]).forEach(([k, v], i) => { if ((i % 2) === 0) { newObj[k] = v; } });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.entries
Object.keys.map
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):
I'll break down the provided benchmark for you. **What is being tested?** The benchmark is comparing two approaches to iterate over an object's key-value pairs: `Object.entries` and `Object.keys`.map`. **Options compared:** 1. **Object.entries**: This method returns an array of a given object's own enumerable string keyed property [key, value] pairs. 2. **Object.keys().map**: This approach uses the `Object.keys()` method to get an array of an object's own enumerable property names, and then uses the `map` function to transform each key into an array of [key, value] pairs. **Pros and Cons:** * **Object.entries**: + Pros: - More concise and expressive syntax. - Returns both keys and values in a single array, making it easier to process them together. + Cons: - May be slower for very large objects, as `Object.keys()` can return an array of all property names, even if they are not used. * **Object.keys().map**: + Pros: - Can be faster for very large objects, as it only returns the keys and allows for more control over processing them. + Cons: - Requires using a map function to transform each key into an array of [key, value] pairs, which can add overhead. **Library usage:** In this benchmark, no external libraries are explicitly mentioned. However, `window.parentObj` is used as an object, suggesting that the benchmark is running in a browser environment where `parent` and `child` windows can be referenced through `window.parent`. **Special JS feature or syntax:** None are explicitly mentioned. **Benchmark preparation code:** The script preparation code generates a random string (`makeid()`) and assigns it as a key to an object, repeated 100 times. This creates a large object with many properties, which is used for the benchmark. **Alternative approaches:** Other ways to iterate over an object's key-value pairs could include: * Using `for...in` loop * Using `Object.getOwnPropertyNames()` and then processing each property name individually * Using a library like Lodash or Ramda to perform the iteration Note that these alternatives may have different performance characteristics and trade-offs compared to `Object.entries` and `Object.keys().map`.
Related benchmarks:
Map vs Array vs Object set uint32 key speed
Map vs Array vs Object has uint32 key speed
Map vs Array vs Object set uint32 key speed11
Object keys vs Array map v2
entries vs keys lookup
Comments
Confirm delete:
Do you really want to delete benchmark?