Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object mapping
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce (reuse object) vs R.mapObjIndexed
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="//cdn.jsdelivr.net/npm/ramda@latest/dist/ramda.min.js"></script>
Script Preparation code:
var data = { ...Array.from(Array(10000).keys()) };
Tests:
Object.fromEntries
const x = Object.fromEntries(Object.entries(data).map((key, value) => [key, value*2])); console.log(x)
Reduce (reuse object)
const x = Object.entries(data).reduce((acc, [k, v]) => { acc[k] = v*2 return acc; }, {}); console.log(x)
R.mapObjIndexed
const x = R.mapObjIndexed((value, key) => value * 2, data) console.log(x)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce (reuse object)
R.mapObjIndexed
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/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromEntries
268.3 Ops/sec
Reduce (reuse object)
2777.6 Ops/sec
R.mapObjIndexed
6094.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases to understand what's being tested. **Benchmark Overview** The benchmark measures performance differences between three approaches for mapping over an object in JavaScript: 1. `Object.fromEntries` 2. `Reduce` (with reusing an existing object) 3. `R.mapObjIndexed` (from the Ramda library) **Script Preparation Code** The script preparation code creates a large data array with 10,000 elements, each with a unique key and value. **Html Preparation Code** The HTML preparation code includes a script tag that loads the latest version of the Ramda library, which is used by two of the test cases (`R.mapObjIndexed`). **Test Cases** There are three individual test cases: 1. **Object.fromEntries**: This test case uses `Object.fromEntries` to create a new object from an array of key-value pairs. 2. **Reduce (reuse object)**: This test case uses the `Reduce` method with a callback function to iterate over the data array, reusing an existing object to store intermediate results. 3. **R.mapObjIndexed**: This test case uses the `mapObjIndexed` function from the Ramda library to create a new object from the data array. **Comparison** The three approaches are compared in terms of their performance, measured by the number of executions per second (ExecutionsPerSecond). **Pros and Cons** Here's a brief summary of each approach: * **Object.fromEntries**: Pros: concise and easy to read. Cons: may incur additional overhead due to object creation. * **Reduce**: Pros: allows reusing an existing object, reducing memory allocation. Cons: can be more complex and harder to read due to the callback function. * **R.mapObjIndexed**: Pros: provides a more functional programming style, leveraging Ramda's optimized implementation. Cons: requires loading an additional library. **Other Considerations** * The benchmark assumes that `Object.fromEntries` is used for creating new objects, while `Reduce` might be used for other purposes, such as aggregating values or sorting. * The use of Ramda's `mapObjIndexed` function introduces an additional dependency and potential performance overhead due to library loading. **Alternatives** If you're looking for alternative approaches, consider: * Using a library like Lodash, which provides similar functionality to Ramda but with more extensive feature sets. * Implementing your own mapping functions using built-in JavaScript methods, such as `forEach`, `map`, and `reduce`. * Leveraging modern JavaScript features like template literals or object destructuring for concise and expressive code. Keep in mind that benchmark results may vary depending on specific use cases, hardware, and software configurations. It's essential to consider these factors when choosing an approach for your project.
Related benchmarks:
ramda clone vs spread
Ramda values vs Object.values
Ramda map latest vs native Array.map
Ramda values vs Object.values 0.27
ramda vs normal range 5000
Comments
Confirm delete:
Do you really want to delete benchmark?