Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
groupConsecutiveNumbers, large and small arrays
(version: 1)
Comparing performance of:
imperative (large arrays) vs functional (large arrays) vs imperative (small arrays) vs functional (small arrays)
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
const reducerForGroupingConsecutiveNumbers = (accumulator, currentNumber, index) => { if (index === 0) { accumulator.push([currentNumber]); } else { const previousGroup = accumulator[accumulator.length - 1]; const previousNumber = previousGroup[previousGroup.length - 1]; if (currentNumber === previousNumber + 1 || currentNumber === previousNumber) { previousGroup.push(currentNumber); } else { accumulator.push([currentNumber]); } } return accumulator; }; /** * Will create an array of subarrays of consecutive numbers * Given: [0,1,2,5,6,9]; * Returns: [[0,1,2],[5,6],[9]] */ function createGroupingsOfConsecutiveNumbers(array) { return array.reduce(reducerForGroupingConsecutiveNumbers, []); }; /** Groups consecutive input values, e.g. [1, 2, 4] -> [[1, 2], [4]] */ function groupConsecutiveNumbers(inputArray) { const initialValue = [[Number()]]; // Use a non-empty initial value to avoid edge cases: https://www.conjur.org/blog/special-cases-are-a-code-smell/ const groupings = inputArray.reduce((accumulator, newNumber) => { /* last(accumulator) and last(last(accumulator)) will always be defined because * 1. its initial value is an array containing an array with at least one element and * 2. we never add array with fewer than 1 elements * They can therefore be safely casted as defined */ const lastSubarray = _.last(accumulator); const lastNumber = _.last(lastSubarray); const isConsecutive = newNumber === lastNumber + 1 || newNumber === lastNumber; if (isConsecutive) { return [...accumulator.slice(0, -1), [...lastSubarray, newNumber]]; } return [...accumulator, [newNumber]]; }, initialValue); const [initialValueGroup, ...rest] = groupings; const withoutInitialValue = [initialValueGroup.slice(1), ...rest].filter((array) => array.length > 0); return withoutInitialValue; }
Tests:
imperative (large arrays)
const numbersFromOneUpToTenThousandNotDivisibleByThree = [...Array(10000).keys()].filter(s => s % 3 !== 0); createGroupingsOfConsecutiveNumbers(numbersFromOneUpToTenThousandNotDivisibleByThree)
functional (large arrays)
const numbersFromOneUpToTenThousandNotDivisibleByThree = [...Array(10000).keys()].filter(s => s % 3 !== 0); groupConsecutiveNumbers(numbersFromOneUpToTenThousandNotDivisibleByThree)
imperative (small arrays)
const smallArray = [0, 1, 2, 5, 6, 9]; createGroupingsOfConsecutiveNumbers(smallArray)
functional (small arrays)
const smallArray = [0, 1, 2, 5, 6, 9]; groupConsecutiveNumbers(smallArray)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
imperative (large arrays)
functional (large arrays)
imperative (small arrays)
functional (small arrays)
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 and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Overview** The benchmark measures the performance of two functions: `createGroupingsOfConsecutiveNumbers` (imperative) and `groupConsecutiveNumbers` (functional). Both functions group consecutive numbers in an array. The benchmark tests these functions on small and large arrays, using both imperative and functional programming styles. **What's being tested** The benchmark measures the execution speed of the two functions: 1. **Imperative style**: `createGroupingsOfConsecutiveNumbers` uses a loop to iterate over the input array and group consecutive numbers. 2. **Functional style**: `groupConsecutiveNumbers` uses the `reduce()` method, which applies a reduction function to each element in the array. **Comparison of options** The benchmark compares two approaches: 1. **Imperative style**: The imperative approach uses a loop to iterate over the input array and group consecutive numbers. 2. **Functional style**: The functional approach uses the `reduce()` method, which applies a reduction function to each element in the array. **Pros and Cons of each approach** **Imperative style (createGroupingsOfConsecutiveNumbers)** Pros: * Easier to understand and debug for some developers * Can be more efficient for small arrays or specific use cases Cons: * More verbose code * May not be as scalable for large datasets **Functional style (groupConsecutiveNumbers)** Pros: * Concise and expressive code * Can be more scalable for large datasets * Uses standard library functions, making it easier to maintain and update Cons: * May be less intuitive or harder to understand for some developers * Can be slower due to the overhead of functional programming **Other considerations** * **Lodash**: The benchmark uses Lodash, a popular utility library that provides functions like `_.last()` and `_.reduce()`. These functions are used in the functional approach to group consecutive numbers. * **Array methods**: Both approaches use array methods (e.g., `reduce()`) to process the input data. This choice of method can affect performance and readability. * **Data size**: The benchmark tests both small and large arrays, which allows for a comparison of performance at different scales. **Alternatives** Other alternatives could include: * Using a different programming paradigm (e.g., object-oriented instead of functional) * Implementing the grouping logic from scratch without using standard library functions * Using a different data structure or algorithm to group consecutive numbers Keep in mind that these alternatives may not be as efficient, readable, or maintainable as the current implementation.
Related benchmarks:
groupConsecutiveNumbers with immutable, large and small arrays
groupConsecutiveNumbers with immutable prepending, large and small arrays
Map or GroupBy
Map or Reduce or Lodash GroupBy Rf
Comments
Confirm delete:
Do you really want to delete benchmark?