Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.prototype.sort (objects vs integers)
(version: 0)
Comparing sort on date property inside object vs sort array of integers
Comparing performance of:
sort array of date property inside objects vs sort array of integers
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function randomDate(start, end) { return new Date(start.getTime() + Math.random() * (end.getTime() - start.getTime())); } var arrObjects = []; for(var i = 0; i < 100000; i++){ arrObjects.push({value:randomDate(new Date(2012, 0, 1), new Date())}); } function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } var arrIntegers = []; for(var i = 0; i < 100000; i++){ arrIntegers.push({value:getRandomInt(100)}); }
Tests:
sort array of date property inside objects
arrObjects.sort((a,b) => a.value - b.value);
sort array of integers
arrIntegers.sort((a,b) => a.value - b.value);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
sort array of date property inside objects
sort array of integers
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
sort array of date property inside objects
25.6 Ops/sec
sort array of integers
410.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the test case and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark measures the performance of `Array.prototype.sort()` on two different scenarios: 1. Sorting an array of objects with a date property (`arrObjects`). 2. Sorting an array of integers (`arrIntegers`). **Script Preparation Code** The script prepares two arrays: `arrObjects` and `arrIntegers`. `arrObjects` contains 100,000 objects with a random date property, while `arrIntegers` contains 100,000 integers. **Html Preparation Code** There is no HTML preparation code provided, which means the benchmark doesn't test any web-specific scenarios. **Individual Test Cases** The benchmark consists of two individual test cases: 1. **sort array of date property inside objects**: The benchmark measures the time it takes to sort `arrObjects` using the custom sorting function `(a, b) => a.value - b.value`. 2. **sort array of integers**: The benchmark measures the time it takes to sort `arrIntegers` using the same custom sorting function. **Libraries and Special Features** There are no libraries used in this benchmark. However, the use of an arrow function (`(a, b) => a.value - b.value`) is noteworthy. This syntax is introduced in ECMAScript 2015 (ES6) and allows for concise, expressive function definitions. **Pros and Cons** The two approaches differ in their performance: * **Sorting array of integers**: This approach should be faster since integer comparisons are generally faster than date comparisons. + Pros: Faster execution time due to the simplicity of integer comparisons. + Cons: May not accurately represent real-world scenarios where dates are involved. * **Sorting array of date property inside objects**: This approach is more representative of real-world scenarios, but may be slower due to the complexity of date comparisons. + Pros: More accurate representation of real-world scenarios where dates are involved. + Cons: Potential for slower execution time. **Other Alternatives** If this benchmark were part of a larger suite, other alternatives might include: * Using a different sorting algorithm (e.g., quicksort, mergesort) * Testing with different array sizes or distribution patterns * Including additional test cases that cover edge cases (e.g., sorting arrays with duplicates) Overall, the benchmark provides valuable insights into the performance characteristics of `Array.prototype.sort()` under different scenarios.
Related benchmarks:
Array.prototype.sort (utc date property vs epoch date simulation property)
Lodash orderBy vs Array.prototype.sort for dates
Lodash 4.17.21 sort vs array.prototype.sort
Lodash sort vs array.prototype.sort vs. sort by key
Comments
Confirm delete:
Do you really want to delete benchmark?