Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for in or reduce
(version: 0)
Comparing performance of:
forInFunc vs funcReduce
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testInitialConfig = { key1: 123, key2: [], key3: { a: 123 } }; var testOverrideConfig = { key1: 'abc', key3: { a: [] } }; var funcReduce = (initialConfig, overrideConfig) => { if (!overrideConfig) { return { ...initialConfig }; } const keys = Object.keys(initialConfig); return keys.reduce( (acc, key) => { if (overrideConfig[key]) { return { ...acc, [key]: { ...initialConfig[key], ...overrideConfig[key], }, }; } return acc; }, { ...initialConfig }, ); }; var forInFunc = (initialConfig, overrideConfig) => { const mergedConfig = { ...initialConfig }; if (!overrideConfig) { return mergedConfig; } for (const keyString in initialConfig) { const key = keyString; if (overrideConfig[key]) { mergedConfig[key] = { ...initialConfig[key], ...overrideConfig[key], }; } } return mergedConfig; };
Tests:
forInFunc
const result1 = forInFunc(testInitialConfig, testOverrideConfig) console.log(result1)
funcReduce
const result2 = funcReduce(testInitialConfig, testOverrideConfig) console.log(result2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forInFunc
funcReduce
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/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forInFunc
113987.4 Ops/sec
funcReduce
88627.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **Benchmark Definition** The benchmark defines two functions: `forInFunc` and `funcReduce`. Both functions are designed to merge two objects, `testInitialConfig` and `testOverrideConfig`, into a single object. The difference between the two functions lies in how they handle merging: 1. **`forInFunc`**: This function uses a traditional `for...in` loop to iterate over the keys of `testInitialConfig`. For each key, it checks if the corresponding value in `testOverrideConfig` is not null or undefined. If so, it merges the two values using the spread operator (`...`). The resulting object is then returned. 2. **`funcReduce`**: This function uses the `Array.prototype.reduce()` method to iterate over the keys of `testInitialConfig`. For each key, it checks if the corresponding value in `testOverrideConfig` exists and has a truthy value (i.e., not null or undefined). If so, it merges the two values using the spread operator (`...`). The resulting object is then returned. **Options Compared** The benchmark compares the performance of these two functions: 1. **`forInFunc`**: Uses a traditional `for...in` loop to iterate over keys. 2. **`funcReduce`**: Uses the `Array.prototype.reduce()` method to iterate over keys. **Pros and Cons** * **`forInFunc`**: + Pros: Easy to understand, straightforward implementation. + Cons: May be slower due to the overhead of the `for...in` loop and string-to-number conversions. * **`funcReduce`**: + Pros: Efficient use of built-in methods, potentially faster execution. + Cons: May be less intuitive for developers unfamiliar with `Array.prototype.reduce()`. **Library/Function Used** The benchmark uses the `Array.prototype.reduce()` method from JavaScript's built-in library. This function is a powerful tool for iterating over arrays and accumulating values, but its use in this context may not be immediately obvious to all developers. **Special JS Feature/Syntax** None mentioned. The benchmark only uses standard JavaScript features and syntax. **Other Considerations** * **Cache locality**: The `forInFunc` loop may exhibit poor cache locality due to the iteration over string keys, which can lead to slower performance. * **Garbage collection**: The use of `var` declarations in the benchmark code may impact garbage collection overhead, potentially affecting performance. **Alternatives** If you wanted to test alternative approaches, you could consider: 1. Using a different iteration method, such as `Object.keys()` or `forEach()`. 2. Implementing the merge logic using a custom loop or recursion. 3. Using a third-party library or framework that provides optimized merging functions. 4. Testing the benchmark with different input configurations to observe variations in performance. Keep in mind that the choice of alternatives will depend on the specific requirements and goals of your benchmark.
Related benchmarks:
Javascript iterate object keys
Object.fromEntries vs reduce vs property assignment
fromEntries vs reduce 2
map-values
reducer vs fromEntries
Comments
Confirm delete:
Do you really want to delete benchmark?