Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sort vs ES5 sort when same functions are added
(version: 0)
Comparing performance of:
_.sortBy vs array.prototype.sort
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
function getRandomInt(max) { return Math.floor(Math.random() * Math.floor(max)); } var arr = []; for (var i = 0; i < 100000; i++) { arr.push({ value: getRandomInt(100) }); }
Tests:
_.sortBy
_.sortBy(arr, (a) => a.value);
array.prototype.sort
[...arr].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
_.sortBy
array.prototype.sort
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 dive into the provided JSON benchmark data. **Overview of the Benchmark** The test is comparing two sorting algorithms: Lodash's `sortBy` function and the built-in `sort` method on JavaScript arrays (ES5). The tests are run on Firefox 109, a desktop browser running Windows. **Options Compared** The two options being compared are: 1. **Lodash's `sortBy` function**: A utility function that sorts an array of objects based on a specified key. In this case, the key is the `value` property. 2. **Built-in `sort` method (ES5)**: A standard method for sorting arrays in JavaScript. **Pros and Cons** ### Lodash's `sortBy` Function Pros: * **Convenience**: Lodash provides a convenient way to sort arrays of objects by a specified key, without having to write custom implementation. * **Performance**: In this benchmark, Lodash's `sortBy` function appears to be faster than the built-in `sort` method. Cons: * **Dependency on Lodash library**: The test requires an instance of the Lodash library to be included in the HTML file. This may not be desirable for all testing environments. * **Overhead**: Using a third-party library like Lodash can introduce additional overhead due to loading times and potential conflicts with other libraries. ### Built-in `sort` Method (ES5) Pros: * **Native performance**: The built-in `sort` method is likely to have native performance, as it's implemented in the browser's JavaScript engine. * **No dependencies**: No external library needs to be included, making it a more straightforward choice for testing. Cons: * **Implementation complexity**: Writing a custom sorting algorithm can be complex and error-prone, especially when dealing with edge cases or large datasets. * **Performance variability**: The performance of the built-in `sort` method may vary depending on the browser's implementation, which could affect benchmark results. **Library Used: Lodash** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as string manipulation, array manipulation, and more. In this case, the `sortBy` function is used to sort arrays of objects based on a specified key. **Special JS Feature/Syntax** None mentioned in the provided JSON data. However, it's worth noting that some modern JavaScript features like async/await or template literals might not be supported in older browsers, which could affect benchmark results. **Other Alternatives** For sorting algorithms, there are other alternatives to Lodash's `sortBy` function and the built-in `sort` method, such as: * **Stable sorting algorithms**: Like Merge Sort, Quick Sort, or Heap Sort, which have a time complexity of O(n log n) but can be more complex to implement. * **Custom implementation**: Writing a custom sorting algorithm from scratch, which can provide better control over performance and optimization. * **Other libraries**: Such as jQuery's `sort` method or other specialized libraries like Ramda. For testing JavaScript benchmarks, there are also other alternatives to MeasureThat.net, such as: * **Jasmine**: A popular JavaScript testing framework for unit tests and integration tests. * **Mocha**: Another widely used JavaScript testing framework that supports asynchronous tests and more. * **Benchmark.js**: A lightweight benchmarking library that provides a simple way to create and run benchmarks.
Related benchmarks:
Check speed of lodash sort vs js sort
Lodash sort vs array.prototype.sort - 2
Lodash orderBy() vs array.prototype.sort
Lodash sort vs array.prototype.sort with localeCompare
Comments
Confirm delete:
Do you really want to delete benchmark?