Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
_.sortBy vs JS sort
(version: 0)
Comparing performance of:
Native JS sort vs lodash sort
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Script Preparation code:
var arr = []; for (var i = 0; i < 1_000_000; i++) { arr.push({ value: Math.random() }); }
Tests:
Native JS sort
arr.sort((a, b) => a.value - b.value);
lodash sort
_.sortBy(arr, (arr) => arr.value);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Native JS sort
lodash sort
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Native JS sort
20.0 Ops/sec
lodash sort
8.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Definition** The benchmark measures the performance difference between two approaches: native JavaScript sort and Lodash's `sortBy` function. The test case creates an array of 1 million objects with random values, sorts it using both methods, and compares their execution times. **Options Compared** Two options are compared: 1. **Native JS Sort**: This is the built-in sorting algorithm provided by JavaScript. It uses a stable sorting algorithm called Timsort, which was introduced in ECMAScript 2015 (ES6). Timsort has a worst-case time complexity of O(n log n), making it suitable for large datasets. 2. **Lodash Sort**: Lodash is a popular JavaScript library that provides various utility functions, including `sortBy`. `sortBy` uses the same Timsort algorithm as native JS sort but with additional optimizations and features like stability and parallelization. **Pros and Cons** Here's a brief summary of each approach: * **Native JS Sort**: + Pros: - Built-in functionality, no external dependency. - Typically faster due to optimized Timsort implementation. - More predictable performance. + Cons: - Limited control over sorting parameters (e.g., stability). - May not work as expected with certain edge cases or library interactions. * **Lodash Sort**: + Pros: - Provides more flexibility and control over sorting parameters. - Can handle non-standard data types and edge cases. - Often faster than native JS sort for large datasets. + Cons: - Requires an external dependency (Lodash). - May introduce overhead due to the library's complexity. **Library: Lodash** Lodash is a widely used JavaScript library that provides a collection of high-quality, customizable functions for various tasks. In this benchmark, `sortBy` is used to sort the array, which leverages Timsort under the hood. The `sortBy` function takes an additional callback parameter to specify the sorting key. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other Alternatives** If you're interested in exploring alternative sorting algorithms, here are a few options: * **Quicksort**: A fast and efficient sorting algorithm with an average time complexity of O(n log n). * **Merge Sort**: Another stable sorting algorithm with a worst-case time complexity of O(n log n). * **Heap Sort**: An in-place sorting algorithm with a time complexity of O(n log n) but can be slower for large datasets due to the overhead of heap operations. For more advanced and performance-critical scenarios, consider exploring libraries like [fast-sort](https://github.com/isaacsmiles/fast-sort) or [sort-recursive](https://github.com/wellcaffeinated/sort-recursive), which offer optimized implementations of various sorting algorithms.
Related benchmarks:
Check speed of lodash sort vs js sort
_.sortBy vs _.orderBy vs JS sort
_.sortBy vs _.orderBy vs JS sort multiple
Compount Mapping and Sorting (lodash vs native)
Comments
Confirm delete:
Do you really want to delete benchmark?