Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array compaction v2
(version: 0)
Comparing performance of:
State machine vs Simple loop vs Simple loop (store prev)
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 collapsePrev(array) { const result = []; let prev = ''; for (const n of array) { if (n !== '*') { result.push(prev + n); prev = ''; } else { prev = '*'; } } return result; } 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 (store prev)
const result = collapsePrev(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 (store prev)
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 provided JSON and explain what is tested in each benchmark. **Benchmark Definition** The benchmark definition represents a JavaScript function that compacts an array of asterisks (`*`) and digits, removing consecutive `*` characters from the array. The three functions implemented are: 1. `trimAsterisks(data)`: This function uses a state machine approach to compact the array. It iterates through the input data, and whenever it encounters a `*`, it marks the previous character as a part of the asterisk sequence. 2. `collapse(data)`: This function uses a simple loop approach to compact the array. It iterates through the input data, and whenever it encounters a `*`, it prepends the previous digit to the current position, effectively removing consecutive `*` characters. 3. `collapsePrev(data)`: This function is similar to `collapse(data)` but stores the previous character in a variable instead of directly concatenating it. **Options Compared** The three functions are compared on the same input data, allowing for a fair comparison of their performance. Pros and Cons: * `trimAsterisks(data)`: This approach has an advantage when dealing with long sequences of asterisks, as it only requires keeping track of the previous character. However, it may be slower due to the state machine implementation. * `collapse(data)`: This approach is simpler and faster than `trimAsterisks(data)` but might be less efficient for long sequences of asterisks. * `collapsePrev(data)`: This approach stores the previous character in a variable, which can improve performance by avoiding unnecessary concatenations. However, it may require more memory to store the previous characters. **Library** The provided benchmark code does not use any external libraries. It only relies on built-in JavaScript functions and data structures. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** Other alternatives to optimize array compaction could include: * Using a different algorithm, such as the Boyer-Moore algorithm * Utilizing parallel processing or multi-threading to take advantage of multiple CPU cores * Optimizing memory allocation and deallocation to minimize overhead * Leveraging compiler optimizations, such as loop unrolling or dead code elimination Keep in mind that these alternatives might require significant changes to the implementation and may not always lead to improved performance. As for the latest benchmark results, they indicate that `collapsePrev(data)` is the fastest, followed by `collapse(data)`, and then `trimAsterisks(data)`.
Related benchmarks:
flatten reduce vs for
Spred vs Slice
Array compaction
Array compaction v3
Comments
Confirm delete:
Do you really want to delete benchmark?