Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Large array to object mapping
(version: 0)
Comparing performance of:
Object spread vs Object assign vs Object fromEntries vs forEach vs for loop
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
array = Array.from(Array(10000).keys())
Tests:
Object spread
const resp = array.reduce( (acc, curr) => ({ ...acc, [curr]: curr }), {}, );
Object assign
const resp = array.reduce( (acc, curr) => Object.assign(acc, { [curr]: curr }), {} );
Object fromEntries
const resp = Object.fromEntries(array.map(curr => [curr, curr]));
forEach
const resp = {}; array.forEach(curr => resp[curr] = curr);
for loop
const resp = {}; for (const curr of array) { resp[curr] = curr; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Object spread
Object assign
Object fromEntries
forEach
for loop
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):
I'm ready to dive into the details of the MeasureThat.net benchmark. **Benchmark Definition** The benchmark definition is a JSON object that describes the test case. In this case, it's a simple example where an array of 10,000 elements is mapped to an object. The script preparation code initializes an array with 10,000 keys using `Array.from()` and `Array(10000).keys()`. There's no HTML preparation code provided. **Test Cases** There are four test cases: 1. **Object Spread**: This test case uses the spread operator (`{ ...acc, [curr]: curr }`) to create a new object from an accumulator object. 2. **Object Assign**: This test case uses `Object.assign()` to assign values to an object. 3. **Object fromEntries**: This test case uses `Object.fromEntries()` to create an object from an array of key-value pairs. 4. **forEach**: This test case uses a traditional `for` loop with `array.forEach()` to iterate over the array and assign values to an object. **Comparison of Options** Here's a brief overview of each option: * **Object Spread**: This approach is concise and efficient, as it creates a new object without modifying the original accumulator. However, it may be less readable for developers unfamiliar with this syntax. * **Object Assign**: This approach is more explicit and easy to understand, but it requires creating an empty object first, which can lead to additional overhead. * **Object fromEntries**: This approach is similar to Object Spread, but uses `Object.fromEntries()` instead of the spread operator. It's a bit less efficient due to the extra function call. * **forEach**: This approach uses a traditional loop and may be slower than the other options due to the overhead of the loop. **Pros and Cons** Here are some pros and cons for each option: * **Object Spread**: + Pros: Concise, efficient + Cons: May be less readable for developers unfamiliar with this syntax * **Object Assign**: + Pros: Easy to understand, explicit + Cons: Additional overhead due to creating an empty object * **Object fromEntries**: + Pros: Similar to Object Spread, but uses `Object.fromEntries()` + Cons: Slightly less efficient due to the extra function call * **forEach**: + Pros: Easy to understand, traditional loop + Cons: May be slower than other options **Library** None of these test cases rely on any external libraries. The `Object.assign()` method is a built-in JavaScript method. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in these test cases. They all use standard JavaScript constructs, such as the spread operator (`{ ... }`), `Object.assign()`, and `forEach()` loops. **Other Alternatives** If you want to try alternative approaches, here are a few options: * **Array.prototype.reduce()**: You can also use `array.reduce()` to create an object from an array. This approach is similar to Object Spread but uses a different method. * **Map**: Instead of using an object or array, you can use a `Map` data structure to store the key-value pairs. * **Other libraries**: If you want to explore more advanced approaches, you could consider using libraries like Lodash or Ramda, which provide additional utility functions for working with arrays and objects. Keep in mind that these alternatives may introduce additional overhead or complexity, so it's essential to evaluate their performance and trade-offs before choosing an alternative approach.
Related benchmarks:
reduce (immutable) vs map + fromEntries
10k: Test Object.values vs array map for creating object array list
iterating from a filled object VS iterating from a map
array includes vs object key lookup, large arrays
Map convert
Comments
Confirm delete:
Do you really want to delete benchmark?