Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object creation from property array, manual vs Object.fromEntries
(version: 0)
Comparing performance of:
Manual vs Object.fromEntries vs Object.fromEntries and Array.map
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var properties = Array.from({ length: 1000 }, () => [Math.random().toString(), Math.random()])
Tests:
Manual
const result = {} for (const [key, value] of properties) { result[key] = value + 1 }
Object.fromEntries
const result = Object.fromEntries(properties)
Object.fromEntries and Array.map
const result = Object.fromEntries(properties.map(([key,value]) => [key, value + 1]))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Manual
Object.fromEntries
Object.fromEntries and Array.map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Manual
20039.7 Ops/sec
Object.fromEntries
3650.7 Ops/sec
Object.fromEntries and Array.map
3353.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation. The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The benchmark tests two approaches for creating an object from an array of property-value pairs: manual and using the `Object.fromEntries` method, as well as its combination with `Array.map`. **Manual Approach** In the manual approach, the test creates an object by iterating over the `properties` array using a `for...of` loop. For each iteration, it sets the value of the corresponding property in the object to be one more than the original value. Pros: * Easy to understand and implement * No additional library or syntax required * Can be optimized for specific use cases (e.g., caching) Cons: * May not be as efficient as other approaches due to iteration overhead * Prone to errors if the array length or property names are incorrect **Object.fromEntries Approach** The `Object.fromEntries` method is a built-in JavaScript method that creates an object from an array of key-value pairs. It's a concise and expressive way to create objects. Pros: * Efficient and fast, as it avoids iteration overhead * Reduces the risk of errors due to its type-safe nature Cons: * Requires the use of `Object.fromEntries` method, which might not be familiar to all developers * Might have performance implications for very large arrays or complex property names **Object.fromEntries and Array.map Approach** This approach combines the efficiency of `Object.fromEntries` with the convenience of `Array.map`. It maps each property-value pair in the array to an array of key-value pairs, and then passes this array to `Object.fromEntries`. Pros: * Combines the benefits of both approaches: efficiency and expressiveness * Can be a good choice for complex use cases where manual iteration is not feasible Cons: * Adds an extra layer of complexity due to the use of `Array.map` * Might have performance implications if the mapping function is computationally expensive Now, let's discuss some special JavaScript features or syntax used in this benchmark: * `for...of` loop: a loop construct that allows iterating over arrays and other iterable objects. * `Array.from()`: a method that creates a new array from an iterable object. No special syntax or features are required to run this benchmark, making it accessible to developers with varying levels of JavaScript experience. Other alternatives for creating objects from arrays include: * Using the spread operator (`{ ... }`) and assigning the result to an object. * Utilizing libraries like Lodash's `fromPairs()` function. * Implementing a custom solution using recursion or a different iteration approach. Keep in mind that these alternatives might have their own pros and cons, and may not be as efficient or convenient as the approaches tested on MeasureThat.net.
Related benchmarks:
Object.keys.length vs JSON.stringify 2
Object.fromEntries vs create temp object
computed property names vs create, assign
javascript new vs Object.create 3
Empty an object in JavaScript (with baseline)
Comments
Confirm delete:
Do you really want to delete benchmark?