Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash sort vs foreach
(version: 0)
Comparing performance of:
_.sortBy vs array.prototype.sort
Created:
6 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,"value");
array.prototype.sort
for(var i=0; i<arr.length; i++) { }
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:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) Gecko/20100101 Firefox/120.0
Browser/OS:
Firefox 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.sortBy
17.3 Ops/sec
array.prototype.sort
33765.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents the benchmark, which compares two approaches for sorting an array: Lodash's `_.sortBy()` function and the built-in `array.prototype.sort()` method. **Benchmark Definition** The benchmark definition consists of two test cases: 1. `_.sortBy(arr,"value")`: This test case uses Lodash's `_.sortBy()` function to sort the `arr` array based on the `value` property. 2. `for(var i=0; i<arr.length; i++) { ... }`: This test case uses the built-in `array.prototype.sort()` method to sort the `arr` array. **Options Compared** The two approaches compared in this benchmark are: 1. **Lodash's `_sortBy()` function**: This function takes an array and a sorting criteria (in this case, `"value"`), and returns a new sorted array. 2. **Built-in `array.prototype.sort()` method**: This method sorts the elements of an array in place, returning the sorted array. **Pros and Cons** Here are some pros and cons of each approach: * **Lodash's `_sortBy()` function**: + Pros: - More concise and expressive syntax - Can handle complex sorting criteria + Cons: - Requires an additional library (Lodash) - May have performance overhead due to the use of a separate function * **Built-in `array.prototype.sort()` method**: + Pros: - Native performance and optimization - Does not require any additional libraries + Cons: - More verbose syntax required for sorting criteria - May not handle complex sorting criteria as easily **Library: Lodash** Lodash is a popular JavaScript library that provides a variety of utility functions, including `_.sortBy()`. The purpose of this function is to sort an array based on a specified property. **Special JS Features/Syntax** This benchmark does not use any special JavaScript features or syntax. Both approaches are standard JavaScript and do not rely on any esoteric features. **Other Alternatives** If you were to create your own benchmarking framework, here are some alternatives to consider: * **Native `Array.prototype.sort()` method with custom compare function**: This approach would use the built-in `sort()` method, but with a custom compare function to handle sorting criteria. * **Using `BigInt` for sorting**: If you need to sort large numbers or other complex data types, using `BigInt` and the built-in `sort()` method could be an alternative. Overall, this benchmark provides a clear comparison between two approaches for sorting an array in JavaScript. The results of this benchmark can help developers understand the performance characteristics of each approach and make informed decisions about which one to use in their own code.
Related benchmarks:
Lodash sort vs array.prototype.sort - 2
Lodash sortBy vs array.prototype.sort small array
Lodash orderBy() vs array.prototype.sort
Lodash sort vs array.prototype.sort with localeCompare
Lodash sort vs array.prototype.sort 3
Comments
Confirm delete:
Do you really want to delete benchmark?