Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce with for vs. native reduce
(version: 0)
Comparing performance of:
function vs native
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var stuff = [ { name: 'pi', value: 3.14 }, { name: 'hundred', value: 100 }, { name: 'truthy', value: true }, { name: 'falsy', value: false }, { name: 'estring', value: '2.71828' }, ], reduce = (array, fn, initial) => { const {length} = array; for (let i = 0; i < length; i += 1) { initial = fn(initial, array[i], i, array); } return initial; }, iterate = (acc, item) => { acc[item.name] = item.value return acc }
Tests:
function
reduce(stuff, iterate, {})
native
stuff.reduce(iterate, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
function
native
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):
**What is tested?** MeasureThat.net is testing two different approaches to reduce an array in JavaScript: the native `reduce` method and a custom implementation using a for loop (`iterate`). The test case uses an array of objects with various properties, including numbers, booleans, and strings. **Options compared:** * **Native `reduce`:** This approach uses the built-in `reduce` method provided by JavaScript's standard library. It takes three arguments: the array to be reduced, a callback function that combines two elements of the array, and an optional initial value. * **Custom implementation (`iterate>`):** This approach uses a for loop to iterate over the array and accumulates the values in an object. **Pros and Cons:** * **Native `reduce`:** + Pros: - More concise and expressive code - Optimized by JavaScript's engine for performance + Cons: - May have slower performance due to overhead of built-in function call - Less control over iteration and accumulation process * **Custom implementation (`iterate>`):** + Pros: - More control over iteration and accumulation process - Can be optimized for specific use cases or hardware + Cons: - More verbose code - May have slower performance due to manual loop optimization **Library usage:** None of the provided test cases use any external libraries. **Special JavaScript features/syntax:** The `reduce` method is a built-in JavaScript feature, but it relies on the execution context and variable scoping rules. The custom implementation (`iterate>`) uses a for loop with a manual index variable `i`. **Benchmark considerations:** * **Hardware:** MeasureThat.net's test results are run on a Mac OS X 10.15.7 desktop machine with Chrome 109. * **JavaScript engine optimization:** Both approaches benefit from optimizations made by JavaScript engines, such as just-in-time (JIT) compilation and caching. **Other alternatives:** For testing array reduction, other options could include: * Using the `forEach` method instead of `reduce`, which can be more intuitive for simple accumulation tasks. * Implementing a custom iterative algorithm using techniques like prefix sum or suffix sum. * Comparing different sorting algorithms (e.g., quicksort, mergesort) on a large dataset. Keep in mind that these alternatives may not provide the same level of performance optimization as the native `reduce` method.
Related benchmarks:
lodash/keyBy vs native reduce 2
lodash/keyBy vs native reduce #3
lodash/keyBy vs native reduce vs map
lodash/keyBy vs native reduce vs Object.groupBy 2
Comments
Confirm delete:
Do you really want to delete benchmark?