Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Two Arrays to Object #3
(version: 2)
Array with keys and Array with values into Object.
Comparing performance of:
forEach #1 vs fromEntries and map #1 vs Object.assign and map #1 vs reduce #1 vs for vs forEach #2 vs fromEntries and map #2 vs Object.assign and map #2 vs reduce #2
Created:
6 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
keys = ["a","b","c","d"]; values = [1,2,3,4];
Tests:
forEach #1
const ob = {}; keys.forEach((key, i)=>ob[key]=values[i]);
fromEntries and map #1
const ob = Object.fromEntries( keys.map((key, i)=>[key, values[i]]) );
Object.assign and map #1
const ob = Object.assign( ...keys.map((k, i) => ({[k]: values[i]})) );
reduce #1
const ob = keys.reduce( (o, k, i) => ({...o, [k]: values[i]}), {} );
for
const ob = {}; for (let key=0; key<keys.length; key++) { ob[keys[key]] = values[key]; }
forEach #2
const ob = {}; const fn = (key, i)=>ob[key]=values[i]; keys.forEach(fn);
fromEntries and map #2
const fn = (key, i)=>[key, values[i]]; const ob = Object.fromEntries(keys.map(fn));
Object.assign and map #2
const fn = (k, i) => ({[k]: values[i]}); const ob = Object.assign( ...keys.map(fn) );
reduce #2
const fn = (o, k, i) => ({...o, [k]: values[i]}); const ob = keys.reduce(fn, {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (9)
Previous results
Fork
Test case name
Result
forEach #1
fromEntries and map #1
Object.assign and map #1
reduce #1
for
forEach #2
fromEntries and map #2
Object.assign and map #2
reduce #2
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'll break down the provided benchmark definition and test cases, explaining what's being tested, compared, and discussing their pros and cons. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that tests various methods for creating an object from two arrays: one with keys and another with values. The test aims to determine which approach is the fastest. **Options Compared** Six different approaches are compared: 1. `forEach` loop 2. `fromEntries` method with `map` 3. `Object.assign` method with `map` 4. `reduce` method 5. `for` loop 6. Closure-based approach using `forEach` **Pros and Cons of Each Approach** 1. **forEach**: The simplest approach, but potentially slow due to the function call overhead. 2. **fromEntries**: A built-in method that creates an object from key-value pairs, which is likely to be fast. 3. **Object.assign**: Another built-in method for creating objects, which might be slower than `fromEntries` due to the function call overhead. 4. **reduce**: A concise and efficient approach using a reducer function, which can be faster than other methods. 5. **for**: A basic loop that creates an object by iterating over the arrays, which is likely to be slow. 6. **Closure-based forEach**: An optimized approach using a closure to avoid the function call overhead of `forEach`, making it competitive with `reduce`. **Libraries and Special Features** None of the test cases use any external libraries or special JavaScript features (e.g., Promises, async/await). **Other Alternatives** If you're interested in exploring alternative approaches, here are some: * Using `Array.prototype.reduce()` with a custom reducer function * Utilizing `String.prototype.forEach()` instead of `Array.prototype.forEach()` * Employing `Function.prototype.call()` to call the closure-based `forEach` implementation Keep in mind that these alternatives might not be as optimized or efficient as the original test cases. Now, if you'll excuse me, I need a deep breath...
Related benchmarks:
object create
Merge array of objects with an object
Merge array of objects with an object.
Take two arrays and merge them using an object key (Map vs. object)
Test push spread map2
Comments
Confirm delete:
Do you really want to delete benchmark?