Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array compaction
(version: 0)
Comparing performance of:
State machine vs Simple loop vs Simple loop (reduce)
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function trimAsterisks(data) { return data.reduce( ({ state, output }, val) => val == '*' ? { state: '*', output } : { state: '', output: output.concat(state + val) }, { state: '', output: [] } ).output; } function collapse(array) { const result = []; for (const [i, n] of array.entries()) { if (n !== '*') { result.push(array[i - 1] === '*' ? `*${n}` : n); } } return result; } function collapseReduce(array) { return array.reduce((a, n, i, { [i - 1]: prev }) => { if (n !== '*') { a.push(prev === '*' ? `*${n}` : n); } return a; }, []); } var data = [ '0', '1', '2', '3', '4', '5', '6', '*', '*', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '*', '34', '*', '36', '37', '*', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '*', '54', '*', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '70', '71', '72', '73', '*', '*', '*', '*', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '*', '91', '92', '93', '94', '95', '*', '97', '*', '*', ];
Tests:
State machine
const result = trimAsterisks(data);
Simple loop
const result = collapse(data);
Simple loop (reduce)
const result = collapseReduce(data);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
State machine
Simple loop
Simple loop (reduce)
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):
Let's break down the benchmark and its test cases. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that measures the performance of three different approaches to compact an array containing asterisks (`*`): 1. `trimAsterisks`: A recursive function that iterates over the array, concatenating each element with the asterisk state. 2. `collapse`: An iterative function that uses the `entries()` method to iterate over the array's indices and values. 3. `collapseReduce`: A version of the previous function using the `reduce()` method. The benchmark prepares an input array `data` containing a mix of numbers and asterisks, and then executes each test case by calling one of the three functions on this data. **Test Cases** There are three individual test cases: 1. **State machine**: This test case uses the `trimAsterisks` function. 2. **Simple loop**: This test case uses the `collapse` function. 3. **Simple loop (reduce)**: This test case uses the `collapseReduce` function. **Options Compared** The three functions (`trimAsterisks`, `collapse`, and `collapseReduce`) differ in their approach to compacting the array: * **State machine (trimAsterisks)**: Uses a recursive approach with a single state variable, which can lead to slower performance due to repeated function calls. * **Simple loop (collapse)**: Uses an iterative approach with a straightforward loop structure, which is likely to be faster than the recursive function due to fewer function calls and better cache locality. * **Simple loop (reduce)**: Uses a more concise version of the previous function using `reduce()`, but it may still suffer from some overhead compared to a simple loop. **Pros and Cons** Here are some pros and cons for each approach: * **State machine (`trimAsterisks`)**: * Pros: * Easy to understand and implement. * Cons: * May have slower performance due to repeated function calls. * **Simple loop (`collapse`)**: * Pros: * Faster execution speed due to fewer function calls. * Better cache locality. * Cons: * More complex loop structure may be harder to understand and maintain. * **Simple loop (reduce) (`collapseReduce`)**: * Pros: * More concise code. * Still uses an efficient iterative approach. * Cons: * May have some overhead compared to a simple loop. **Latest Benchmark Result** The latest benchmark result shows that the `Simple loop (reduce)` test case has the highest executions per second, indicating faster performance. However, the `State machine` test case still runs at a relatively high frequency despite being slower in terms of absolute performance. In conclusion, this benchmark highlights the importance of considering both execution speed and code complexity when choosing an approach for array compacting tasks. The concise yet efficient implementation of `collapseReduce` appears to be the winner here, but the differences are relatively small, indicating that further optimization efforts may be necessary to achieve significant improvements in performance.
Related benchmarks:
flatten reduce vs for
Spreading vs Contact in Reduce
Array compaction v2
Array compaction v3
Comments
Confirm delete:
Do you really want to delete benchmark?