Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native reverse find vs native array findLast vs for loop find
(version: 0)
Compare the new ES6 spread operator with the traditional concat() method
Comparing performance of:
array find vs array findlast vs for loop
Created:
one year 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': 'joey', 'age': 32 }, { 'user': 'ross', 'age': 41 }, { 'user': 'joey', 'age': 32 }, { 'user': 'ross', 'age': 41 }, { '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 < 40; })
array findlast
users.slice().findLast(function (o) { return o.age < 40; })
for loop
let lastItem = undefined; for (let i = users.length - 1; i >= 0; i--) { if (users[i].age < 40) { lastItem = users[i]; break; // Exit the loop once the item is found } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
array find
array findlast
for loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
19 days ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array find
21133500.0 Ops/sec
array findlast
28614752.0 Ops/sec
for loop
1063910464.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of three different approaches to find an element in an array with specific conditions: using `array.find()`, `array.findLast()`, and a manual `for` loop. **Test Cases** There are three test cases: 1. **array find**: Uses `slice()` to create a copy of the original array, reverses it, and then finds the first element that satisfies the condition (`o.age < 40`) using the `find()` method. 2. **array findlast**: Uses `slice()` to create a copy of the original array, finds the last element that satisfies the condition (`o.age < 40`) using the `findLast()` method. 3. **for loop**: Iterates over the array in reverse order and checks each element's age until it finds one that is less than 40. **Options Compared** The benchmark compares the performance of: * Using `array.find()` (Test Case 1) * Using `array.findLast()` (Test Case 2) * A manual `for` loop (Test Case 3) **Pros and Cons of Each Approach** 1. **array find**: * Pros: Simple, readable, and easy to implement. * Cons: Creates a copy of the array, which can be memory-intensive for large datasets. 2. **array findlast**: * Pros: More efficient than `find()` since it only returns the last element that satisfies the condition, rather than searching the entire array. * Cons: May not work correctly if the array is empty or contains multiple elements with the same condition (in this case, no such issues). 3. **for loop**: * Pros: Highly customizable and can be optimized for specific use cases. * Cons: More verbose, error-prone, and may require additional memory to store variables. **Libraries Used** The benchmark uses two JavaScript libraries: 1. **Lodash**: The `find()` and `findLast()` methods are part of Lodash's utility functions. Lodash is a popular utility library that provides a wide range of functions for tasks like array manipulation, functional programming, and more. 2. **No additional libraries** are used in the manual `for` loop implementation. **Special JavaScript Features/Syntax** The benchmark uses the following special JavaScript features: 1. **Spread operator (`...`)**: Used in the `slice()` method to create a copy of the original array. 2. **Arrow functions**: Used in the `find()` and `findLast()` methods to define small, anonymous functions. **Alternatives** Other approaches to find an element in an array with specific conditions might include: * Using `filter()`: Instead of finding a single element, you can use `filter()` to create a new array with all elements that satisfy the condition. * Using a custom implementation with a separate function for each iteration: This approach would require manual memory management and may be more error-prone than using built-in methods like `find()` or `for` loops. Keep in mind that the choice of approach depends on the specific requirements and constraints of your use case.
Related benchmarks:
normal function find vs arrow function find
native reverse find vs lodash _.findLast larger sample
native reverse find vs native array findLast
native find vs for..in
Comments
Confirm delete:
Do you really want to delete benchmark?