Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
groupConsecutiveNumbers with immutable, large and small arrays
(version: 0)
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/immutable/4.0.0-rc.12/immutable.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, []); }; const { List } = Immutable; /** Groups consecutive input values, e.g. [1, 2, 4] -> [[1, 2], [4]] */ function groupConsecutiveNumbers(inputArray) { const initialValue = List.of(List.of(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 = accumulator.last(); const lastNumber = lastSubarray.last(); const isConsecutive = newNumber === lastNumber + 1 || newNumber === lastNumber; if (isConsecutive) { return accumulator.slice(0, -1).push(lastSubarray.push(newNumber)); } return accumulator.push(List([newNumber])); }, initialValue) .toJS(); 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):
Measuring performance of different approaches to group consecutive numbers is an interesting benchmark. Let's break down what's being tested and the pros and cons of each approach. **What's being tested:** Two functions, `createGroupingsOfConsecutiveNumbers` (imperative) and `groupConsecutiveNumbers` (functional), are being compared for their performance when grouping consecutive numbers in an array. The functions take a large array of numbers as input and return an array of subarrays containing consecutive numbers. **Approach 1: Imperative (createGroupingsOfConsecutiveNumbers)** This approach uses a reducer function to iterate through the input array and group consecutive numbers. It maintains a running total of consecutive numbers in an accumulator, which is updated on each iteration. Pros: * Easy to understand and implement for developers familiar with imperative programming. * Can be optimized using various techniques, such as caching intermediate results or using a more efficient data structure. Cons: * May have higher overhead due to the need to explicitly manage the accumulator and update it on each iteration. * Can be less efficient than functional approaches for large datasets, as it requires more CPU cycles to process the input array. **Approach 2: Functional (groupConsecutiveNumbers)** This approach uses a functional programming style to group consecutive numbers. It uses Immutable.js library to handle immutability and create a pipeline of operations to transform the input array into an array of subarrays containing consecutive numbers. Pros: * Encourages a more declarative programming style, which can lead to more concise and maintainable code. * Can be optimized using functional programming techniques, such as memoization or caching intermediate results. * Often faster than imperative approaches for large datasets, as it avoids the overhead of explicit accumulation and iteration. Cons: * May require additional setup and knowledge of functional programming concepts, such as Immutable.js. * Can be less intuitive for developers unfamiliar with functional programming. **Other considerations:** * The `numbersFromOneUpToTenThousandNotDivisibleByThree` array is used to test the performance of both functions. This creates a large dataset that's optimized for benchmarking, ensuring that the results are accurate and representative. * The `smallArray` tests are used to compare the performance of both functions with smaller input datasets. These tests are useful for understanding how the functions perform in practice, rather than solely relying on the larger array test. **Benchmark results:** The latest benchmark results show that the functional approach (groupConsecutiveNumbers) is generally faster than the imperative approach (createGroupingsOfConsecutiveNumbers), especially with large input datasets. However, the performance difference may be less pronounced for smaller input datasets like `smallArray`.
Related benchmarks:
Array duplicate removal for duplicates exceeding `N`-number
Array duplicate removal for duplicates exceeding `N`-number
Array duplicate removal for duplicates exceeding `N`-number
Array duplicate removal for duplicates exceeding `N`-number
The Non Repeating Number
Comments
Confirm delete:
Do you really want to delete benchmark?