Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
groupConsecutiveNumbers with immutable prepending, 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 firstSubarray = accumulator.first(); const firstNumber = firstSubarray.first(); const isConsecutive = newNumber === firstNumber + 1 || newNumber === firstNumber; if (isConsecutive) { return accumulator.shift().unshift(firstSubarray.unshift(newNumber)); } return accumulator.unshift(List.of(newNumber)); }, initialValue) .toJS() .map((x) => x.reverse()) .reverse(); 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 benchmark and explain what's being tested, compared options, pros and cons of each approach, library usage, special JavaScript features or syntax, and alternative approaches. **Benchmark Test** The benchmark measures the performance difference between two approaches: 1. **Imperative**: Using a traditional loop (e.g., `for` loop) to group consecutive numbers in an array. 2. **Functional**: Using a functional programming approach with recursion and immutable data structures to group consecutive numbers in an array. **Options Compared** The benchmark compares the performance of two imperative approaches: 1. **createGroupingsOfConsecutiveNumbers**: The original implementation using a custom reducer function. 2. **groupConsecutiveNumbers**: An alternative implementation using Immutable.js, a library for functional programming in JavaScript. The benchmark also compares the performance of two functional approaches: 1. **createGroupingsOfConsecutiveNumbers** (imperative): Using a traditional loop to group consecutive numbers. 2. **groupConsecutiveNumbers** (functional): Using Immutable.js and recursion to group consecutive numbers. **Pros and Cons of Each Approach** * Imperative approach: + Pros: Can be more intuitive for beginners, easier to understand the logic. + Cons: May lead to performance issues due to overhead from managing loops and state. * Functional approach with Immutable.js: + Pros: Can provide better performance and concurrency benefits due to immutable data structures and recursion. + Cons: May require a stronger understanding of functional programming concepts. **Library Usage** The benchmark uses Immutable.js, a library for functional programming in JavaScript. Specifically, it uses the `List` type from Immutable.js to create an initial value with at least one element, which is then used as a starting point for recursion. **Special JavaScript Features or Syntax** There are no special JavaScript features or syntax mentioned in this benchmark that are specific to modern JavaScript versions or ECMAScript standards. However, the use of `const` and template literals (`\r\n`) suggests that the code is written in modern JavaScript syntax. **Alternative Approaches** Other alternative approaches could include: 1. Using a library like Lodash or Ramda for functional programming. 2. Implementing a custom data structure, such as a tree or graph, to group consecutive numbers. 3. Using parallel processing or concurrent execution techniques to compare the performance of different approaches. Overall, this benchmark provides a useful comparison between imperative and functional programming approaches for grouping consecutive numbers in an array, highlighting the benefits and trade-offs of each approach.
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?