Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object rename: reduce vs fromEntries vs assign & delete
(version: 0)
Comparing performance of:
reduce vs fromEntries vs assign & delete
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
obj = { "a": 1, "b": 'string', c: {hi:'ho'} }; prefix = "_";
Tests:
reduce
return Object.keys(obj).reduce(function (result, key) { result[prefix + key] = obj[key]; return result; }, {});
fromEntries
return Object.fromEntries( Object.entries(obj).map(([k, v]) => [`${prefix}${k}`, v]) )
assign & delete
for (const key in obj) { obj[prefix+key] = obj[key]; delete obj[key]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
reduce
fromEntries
assign & delete
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
8471822.0 Ops/sec
fromEntries
4437298.5 Ops/sec
assign & delete
10286.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript benchmark test case, specifically designed to compare the performance of three different approaches for renaming object properties. **Benchmark Definition** The benchmark is defined by a single script that creates an object `obj` with several properties: `"a": 1`, `"b"` (a string), and a nested object `c` with a property `hi`. The script also defines a prefix `_`. **Test Cases** There are three test cases, each representing one of the approaches: 1. **"reduce"`: This approach uses the `Array.prototype.reduce()` method to rename the properties. It iterates over the object's keys, creating a new key-value pair for each property, and returns an empty object. 2. **"fromEntries"`: This approach uses the `Object.fromEntries()` method to create a new object from an array of key-value pairs. The `Object.entries()` method is used to get an array of the object's key-value pairs, which are then mapped to include the prefix in each key. 3. **"assign & delete"`: This approach uses a simple loop to rename the properties by assigning the original value to the new key and then deleting the original key. **Pros and Cons** 1. **"reduce"`: * Pros: efficient use of `Array.prototype.reduce()` method, which is optimized for performance. * Cons: may not be as readable or maintainable due to its concise nature. 2. **"fromEntries"`: * Pros: more readable and maintainable than the other two approaches, uses modern methods that are widely adopted. * Cons: may have slightly higher overhead due to the creation of an array and object. 3. **"assign & delete"`: * Pros: simple and easy to understand, avoids potential pitfalls with `fromEntries`. * Cons: may be slower than the other two approaches due to the repeated assignments and deletions. **Library and Features** No libraries are used in this benchmark. However, some features of JavaScript are implicitly used: 1. **`Array.prototype.reduce()`**: a built-in method for reducing an array to a single value. 2. **`Object.entries()`**: a built-in method for getting an array of key-value pairs from an object. 3. **`Object.fromEntries()`**: a built-in method for creating an object from an array of key-value pairs (available in ECMAScript 2020+). **Alternatives** If you're looking for alternative approaches, consider the following: 1. Using `Object.assign()` to create a new object with renamed properties. 2. Using a library like Lodash (`_`) to rename object properties, which can provide more flexibility and readability. Note that these alternatives may have different performance characteristics compared to the original three approaches.
Related benchmarks:
Delete vs destructure for objects
JSON.stringify vs Destructuring
Delete vs destructure vs reduce for objects
Delete vs destructure for objects without mutating and mutating
Delete vs destructure for objects with and without mutating
Comments
Confirm delete:
Do you really want to delete benchmark?