Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sorted vs unsorted Array sort
(version: 0)
Comparing performance of:
Unsorted vs Sorted with check vs Sorted without check vs Unsorted with check
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr1 = []; for(let i = 0; i < 10000; i++) { arr1.push(Math.floor(Math.random() * 5000)); } var arr2 = arr1.slice(0); arr2.sort((a, b) => a - b);
Tests:
Unsorted
var toSort = arr1.slice(0); toSort.sort((a, b) => a - b);
Sorted with check
var toSort = arr2.slice(0); let lastEntry; let isSorted = true; for(const entry of toSort) { if(lastEntry != null && entry < lastEntry) { isSorted = false; break; } lastEntry = entry; } if(!isSorted) { toSort.sort((a, b) => a - b); }
Sorted without check
var toSort = arr2.slice(0); toSort.sort((a, b) => a - b);
Unsorted with check
var toSort = arr1.slice(0); let lastEntry; let isSorted = true; for(const entry of toSort) { if(lastEntry != null && entry < lastEntry) { isSorted = false; break; } lastEntry = entry; } if(!isSorted) { toSort.sort((a, b) => a - b); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Unsorted
Sorted with check
Sorted without check
Unsorted with check
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Chrome OS 14541.0.0
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Unsorted
216.2 Ops/sec
Sorted with check
23828.3 Ops/sec
Sorted without check
2419.5 Ops/sec
Unsorted with check
175.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **Benchmark Definition** The benchmark measures the performance of three different approaches for sorting an array: 1. **Sorted with check**: This approach sorts the array first, and then checks if it is sorted using a loop. If it's not sorted, the array is sorted again. 2. **Sorted without check**: This approach sorts the array only once, without checking if it is already sorted. 3. **Unsorted with check**: This approach creates an unsorted array, sorts it, and then checks if it is sorted using a loop. If it's not sorted, the array remains unsorted. **Options Compared** The benchmark compares the performance of three different approaches: 1. Sorting an array first (Sorted without check) 2. Checking if an array is already sorted after sorting (Sorted with check) 3. Creating an unsorted array and leaving it that way (Unsorted with check) **Pros and Cons** Here are some pros and cons of each approach: * **Sorted without check**: + Pros: Simple, efficient, and fast + Cons: May be slower than other approaches if the array is already partially sorted or has a small number of elements * **Sorted with check**: + Pros: Can take advantage of partial sorting or small arrays to improve performance + Cons: Requires additional loop overhead to check if the array is sorted, which can slow down performance for large arrays * **Unsorted with check**: + Pros: Can be faster than Sorted without check if the unsorted array has a high degree of randomness + Cons: May not perform well for arrays with a small number of elements or low randomness **Library and Special JS Features** In this benchmark, no libraries are used. However, JavaScript does have some special features that can impact performance: * **Loops**: Loops can be slower than other constructs due to overhead in the V8 engine. * **Array methods**: Array methods like `sort()`, `forEach()`, and `slice()` can be optimized by the V8 engine, but may still incur some overhead. **Alternatives** Other approaches that could be compared in this benchmark include: * Using a different sorting algorithm, such as QuickSort or Merge Sort * Using parallel processing or multi-threading to sort arrays * Optimizing array sorting using specialized libraries like NumJS or MathJS Overall, the benchmark provides a simple and straightforward way to compare the performance of three different approaches for sorting arrays.
Related benchmarks:
Javascript Sorting Algorithmzzz
Javascript Sorting Algorithms large
Array sort & map vs. map & sort
slice sort vs sort
Comments
Confirm delete:
Do you really want to delete benchmark?