Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries + Object.entries vs Object.keys
(version: 0)
Comparing performance of:
Object.fromEntries + Object.entries vs Object.keys
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = {v1: {value: 'string', num: 123, obj: {boo: true, arr: [{str: 'string'},{str: 'string'},{str: 'string'},{str: 'string'},{str: 'string'}]}}, v2: {value: 'string', num: 123, obj: {boo: true, arr: [{str: 'string'},{str: 'string'},{str: 'string'},{str: 'string'},{str: 'string'}]}}}
Tests:
Object.fromEntries + Object.entries
JSON.stringify(Object.fromEntries(Object.entries(obj).map(([key, value]) => [key, {...value, obj: value.obj.arr}])))
Object.keys
const newObj = {} Object.keys(obj).forEach(key => { newObj[key] = { ...obj[key], ccSubset: obj[key]?.ccSubset?.ccSubset }; }); JSON.stringify(newObj)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.fromEntries + Object.entries
Object.keys
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36 Edg/132.0.0.0
Browser/OS:
Chrome 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromEntries + Object.entries
881085.2 Ops/sec
Object.keys
902919.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **What is being tested?** The provided JSON represents two benchmark test cases that compare the performance of creating objects using different methods: 1. `Object.fromEntries + Object.entries` 2. `Object.keys` Both test cases create a deep object with multiple levels of nesting, but they use different approaches to achieve this. **Options being compared:** * **`Object.fromEntries + Object.entries`**: This method involves: + Converting the object's key-value pairs into an array using `Object.entries()`. + Mapping over this array to create a new array with modified objects. + Using `Object.fromEntries()` to convert this new array back into an object. * **`Object.keys`**: This method involves iterating over the object's keys and creating a new object with modified key-value pairs using the `forEach()` method. **Pros and Cons of each approach:** 1. **`Object.fromEntries + Object.entries`**: + Pros: - More concise and expressive syntax. - Can be faster since it uses built-in functions that are optimized for performance. + Cons: - May require more memory to create the temporary array. 2. **`Object.keys`**: + Pros: - Requires less memory to create the temporary objects. + Cons: - Less concise and more verbose syntax. - Can be slower due to the iteration overhead. **Library usage:** None of the test cases use any libraries that are not part of the JavaScript standard library or built-in functions. However, it's worth noting that `Object.fromEntries()` is a relatively new feature introduced in ECMAScript 2019 (ES10). **Special JS features or syntax:** There are no special JS features or syntax used in these benchmark test cases. **Other alternatives:** If you're looking for alternative methods to create deep objects, here are some options: 1. Using `Array.prototype.reduce()` instead of `Object.fromEntries()`. 2. Using a library like Lodash's `mapKeys()` function. 3. Creating the object manually using a loop and the dot notation (e.g., `obj[key] = value;`). However, these alternatives may not be as concise or efficient as the original methods being compared in this benchmark. In summary, the test cases compare two approaches to creating objects with multiple levels of nesting: `Object.fromEntries + Object.entries` and `Object.keys`. The former is more concise but may require more memory, while the latter requires less memory but can be slower due to iteration overhead.
Related benchmarks:
key in object vs object.key
Object.keys() vs Object.values() vs Object.entries()
entries vs keys lookup
Object.keys vs Object.entries vs Object.values
Comments
Confirm delete:
Do you really want to delete benchmark?