Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sort benchmark 2
(version: 1)
Check different approaches for RN sort instability.
Comparing performance of:
FOR_EACH vs FILTER vs FOR
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
window.result window.presetSortArray = Array.from(Array(50).keys()).map(i => ([1, 3].includes((i + 1) % 7) ? i * -1 : (i + 1) * 7))
Tests:
FOR_EACH
window.resultArrayFirstPart = [] window.resultArraySecondPart = [] window.presetSortArray.forEach(element => { if (element <= 0) { window.resultArrayFirstPart.push(element) } else { window.resultArraySecondPart.push(element) } }) window.result = window.resultArrayFirstPart.concat(window.resultArraySecondPart)
FILTER
window.result = window.presetSortArray.filter(element => element < 0).concat(window.presetSortArray.filter(element => element >= 0))
FOR
window.resultArrayFirstPart = new Array(window.presetSortArray.length) window.resultArraySecondPart = new Array(window.presetSortArray.length) window.index1 = 0; window.index2 = 0; for (let index = 0, length = window.presetSortArray?.length; index < length; ++index) { if (window.presetSortArray[index] <= 0) { window.resultArrayFirstPart[++window.index1] = window.presetSortArray[index]; } else { window.resultArraySecondPart[++window.index2] = window.presetSortArray[index]; } } for (let index = 0; index < window.index2; ++index) { window.resultArrayFirstPart[++window.index1] = window.resultArraySecondPart[index]; } window.result = window.resultArrayFirstPart
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
FOR_EACH
FILTER
FOR
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 explain what's being tested. **Benchmark Definition:** The benchmark is testing different approaches for sorting arrays in JavaScript, specifically with regards to stability. The goal is to compare the performance of three different methods: 1. `FOR_EACH` 2. `FILTER` 3. `FOR` **What's being tested?** Each test case measures the execution time of sorting an array using a specific method. * `FOR_EACH`: This approach uses a for-each loop to iterate over the array and assign elements to two separate arrays based on their values. * `FILTER`: This approach uses the Array.prototype.filter() method to create two new arrays: one containing elements with negative values and another containing elements with non-negative values. * `FOR`: This approach uses a traditional for loop to iterate over the array, assigning elements to two separate arrays based on their values. **Pros and Cons of each approach:** 1. **FOR_EACH**: * Pros: Easy to read and write, can be more concise than traditional loops. * Cons: May have performance overhead due to the use of two arrays and additional logic for handling negative values. 2. **FILTER**: * Pros: More concise and expressive than traditional loops, uses built-in Array.prototype method. * Cons: May be slower due to the creation of two new arrays, which can incur memory allocation costs. 3. **FOR**: * Pros: Can be more efficient for large arrays since it avoids creating intermediate arrays. * Cons: More verbose and less concise than the other approaches. **Library Used:** There is no explicit library mentioned in the benchmark definition or test cases. However, some browser-specific features are used: * `window.resultArrayFirstPart` and `window.resultArraySecondPart` use JavaScript's global scope (`window`) to create variables. * Safari's specific version (15) is mentioned. **Special JS Feature/ Syntax:** None of the approaches explicitly utilize any special JavaScript features or syntax beyond what's standard in ES6+. However, some may rely on implicit behavior or performance optimizations that are browser-specific. **Other Alternatives:** For sorting arrays, other alternatives could include: * Using `Array.prototype.sort()` method with a custom comparison function. * Utilizing libraries like Lodash or Ramda for functional programming approaches. * Implementing a homegrown sorting algorithm using techniques like QuickSort or Merge Sort. Keep in mind that the choice of approach depends on the specific requirements, performance constraints, and personal coding style.
Related benchmarks:
Array Sorting Methods
Sorting speed comparison
pre-sorted sorting (int vs obj)
Sort method comparisons (quicksort, for loop, Arra.prototype.sort)
Comments
Confirm delete:
Do you really want to delete benchmark?