Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native reverse find vs lodash _.findLast larger sample
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method
Comparing performance of:
array find vs _.findLast
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var users = [ { 'user': 'joey', 'age': 32 }, { 'user': 'ross', 'age': 41 }, { 'user': 'chandler', 'age': 39 }, { 'user': 'joey', 'age': 32 }, { 'user': 'ross', 'age': 41 }, { 'user': 'chandler', 'age': 39 }, { 'user': 'joey', 'age': 32 }, { 'user': 'rachel', 'age': 25 }, { 'user': 'chandler', 'age': 39 }, { 'user': 'joey', 'age': 32 }, { 'user': 'ross', 'age': 41 }, { 'user': 'chandler', 'age': 39 }, { 'user': 'joey', 'age': 32 }, { 'user': 'ross', 'age': 41 }, { 'user': 'chandler', 'age': 39 }, { 'user': 'joey', 'age': 32 }, { 'user': 'ross', 'age': 41 }, { 'user': 'chandler', 'age': 39 } ]
Tests:
array find
// Native users.slice().reverse().find(function (o) { return o.age < 30; })
_.findLast
_.findLast(users, function (o) { return o.age < 30; })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array find
_.findLast
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
array find
4541406.5 Ops/sec
_.findLast
2197754.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared options, pros and cons, and other considerations. **Benchmark Description** The test aims to compare two approaches: using the new ES6 spread operator (`...`) with the traditional `concat()` method for finding an element in an array. **Script Preparation Code** The script prepares a large array of user objects with varying ages: ```javascript var users = [ { 'user': 'joey', 'age': 32 }, { 'user': 'ross', 'age': 41 }, { 'user': 'chandler', 'age': 39 }, ... ]; ``` The script preparation code is intentionally incomplete to force the use of either `...` or `concat()`. **Html Preparation Code** The HTML includes two JavaScript files: 1. `lodash.core.js`: a minified version of Lodash, which provides functional programming utilities. 2. `lodash.min.js`: another minified version of Lodash, specifically optimized for performance and size. These libraries are used to make the test cases easier to write and maintain. **Individual Test Cases** There are two individual test cases: 1. **"array find"`**: This test case uses the new ES6 spread operator (`...`) to create a reversed copy of the array and then finds an element that meets the condition (`o.age < 30`). 2. **"_.findLast(users, function (o) { return o.age < 30; })"`**: This test case uses Lodash's `findLast` function to achieve the same result as the previous test case. **Test Results** The latest benchmark results show the execution performance of both test cases on a Chrome 121 browser on a Windows desktop: 1. **"array find"`**: 454,1406 executions per second 2. **"_.findLast"`: 219,7754 executions per second **Comparison Options** The two options being compared are: * Using the new ES6 spread operator (`...`) with `concat()` to create a reversed copy of the array. * Using Lodash's `findLast` function. **Pros and Cons** **Spread Operator (`...`) + concat()** Pros: * Uses modern JavaScript features, which may result in better code readability and maintainability. * Can be more efficient for small arrays. Cons: * May not perform well on very large datasets due to the overhead of creating a reversed copy. * Requires two function calls ( `slice()` and `concat()` ). **Lodash's `findLast`** Pros: * Specifically optimized for performance and size, making it suitable for large datasets. * Provides a simple and concise way to find the last element that meets the condition. Cons: * Uses an external library, which may add extra overhead. * May require additional setup or imports in the test code. **Other Considerations** * The use of Lodash's `findLast` function may be seen as less "pure" JavaScript, as it relies on a third-party library. However, this can also be viewed as a trade-off for performance and convenience. * The spread operator (`...`) is still a relatively new feature in JavaScript, and its performance might not be optimal yet. **Alternatives** If you prefer to avoid using external libraries or modern features, you could consider using other approaches, such as: * Using `Array.prototype.indexOf()` or `Array.prototype.lastIndexOf()` to find the index of the last element that meets the condition. * Creating a custom function or utility to achieve the same result. Keep in mind that these alternatives might not provide the same level of performance and convenience as Lodash's `findLast` or the spread operator (`...`) with `concat()`.
Related benchmarks:
native find vs lodash _.find with null values and object
native find vs lodash _.find altered
native reverse find vs native array findLast
native reverse find vs native array findLast vs for loop find
Comments
Confirm delete:
Do you really want to delete benchmark?