Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.keys(obj).reduce versus Array.map().push() (Performance test)
(version: 5)
useForm hook
Comparing performance of:
Object.keys().reduce() vs Array.map().push()
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var obj = { currentPassword: { value: '', required: true, }, newPassword: { value: '123456', required: true, validator: (newPassword) => { if (newPassword.length <= 6) { return 'Passphrase must be at least 6 characters long'; } else return ''; }, }, verifyPassword: { value: '123457', required: true, validator: (verifyPassword, newPassword) => { if (verifyPassword !== newPassword) { return 'Passwords do not match'; } else return ''; }, }, }; var arr = [{ name: 'currentPassword', value: '', required: true, }, { name: 'newPassword', value: '123456', required: true, validator: (newPassword) => { if (newPassword.length <= 6) { return 'Passphrase must be at least 6 characters long'; } else return ''; }, }, { name: 'verifyPassword', value: '123457', required: true, validator: (verifyPassword, newPassword) => { if (verifyPassword !== newPassword) { return 'Passwords do not match'; } else return ''; }, }, ];
Tests:
Object.keys().reduce()
for (var i=1000; i > 0; i--) { var keys = Object.keys(obj); var reduced = keys.reduce((object, key) => { object[key] = obj[key] return object; }, {}); // console.log('obj', reduced); // verify verifyPassword against newPassword via its validator function var errorMessage = obj.verifyPassword.validator(obj.verifyPassword.value, obj['newPassword'].value); // console.log('obj errorMessage', errorMessage); }
Array.map().push()
for (var i = 1000; i > 0; i--) { var newArray = []; arr.map((element) => { newArray.push(element); }); // console.log('arr', newArray); // verify newPassword (newArray[1]) via its validator function var errorMessage = newArray[1].validator('12345'); // console.log('arr errorMessage', errorMessage); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.keys().reduce()
Array.map().push()
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):
Let's break down the benchmark and its options. **Benchmark Definition:** The benchmark is comparing two approaches to iterate over an object and perform some operations on its values: 1. `Object.keys(obj).reduce(...)`: This approach uses the built-in `Object.keys()` method to get an array of the object's keys, and then uses the `Array.prototype.reduce()` method to iterate over the keys and create a new object with the same properties as the original object. 2. `Array.map().push()`: This approach uses the `Array.prototype.map()` method to create a new array from the original array of objects, and then uses the `Array.prototype.push()` method to add each element to the new array. **Options Compared:** The benchmark is comparing the performance of these two approaches under different conditions. The options being compared are: * The size of the object (1000 entries) * The number of iterations (1000 times) **Pros and Cons of Each Approach:** **Object.keys().reduce():** Pros: * More concise and expressive code * Can be more efficient for large datasets since it avoids creating a new array Cons: * May be slower due to the overhead of calling `Object.keys()` and creating an object with the same properties as the original object * Requires JavaScript version 4.5+ (due to the use of `reduce()`) **Array.map().push():** Pros: * Typically faster since it avoids creating a new object and uses optimized native code for array manipulation * More widely supported across different browsers and versions Cons: * May be less concise and expressive code compared to `Object.keys().reduce()` * Creates a new array, which can lead to memory allocation and deallocation overhead **Other Considerations:** * The use of the `validator` function in both approaches is not relevant to the performance comparison. * The benchmark does not consider other factors that might affect performance, such as caching, parallelization, or concurrency. **Library and Special JS Features Used:** There are no libraries used in this benchmark. However, it's worth noting that the use of `reduce()` requires JavaScript version 4.5+, which is a relatively recent feature. If you were to run this benchmark on a different environment or platform, keep in mind that the performance results may vary due to differences in browser versions, operating systems, and hardware configurations. **Alternatives:** Some alternative approaches for iterating over objects include: * Using `for...in` loops with object iteration (although this is generally slower than `Object.keys()` and `reduce()`) * Using a `forEach()` loop or a custom iterator function * Using a library like Lodash or Ramda, which provides optimized functions for common data processing tasks. It's worth noting that the choice of approach ultimately depends on the specific use case and performance requirements.
Related benchmarks:
reduce vs filter 22
Object.fromEntries(array.map) vs. array.map.reduce
Reduce vs map with empty filter
Object.fromEntries(Array.map) vs Array.reduce (with different methods)
reduce object vs object.fromentries (1K records)
Comments
Confirm delete:
Do you really want to delete benchmark?