Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Testing unread id search
(version: 0)
Comparing performance of:
for loop vs _.findLastIndex
Created:
3 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.js"></script>
Script Preparation code:
var list = [ ...Array(100000).keys() ].map((index) => ({ id: index })); var fromIndex = 100000;
Tests:
for loop
const nextUnreadIds = { [0]: null, [1]: null, } const populateNextUnreadIds = (nextUnreadIds, fromIndex, list) => { for (let i = fromIndex; i >= 0; i--) { [0, 1].forEach((id) => { if (nextUnreadIds[id] === null && list[i] === id) { nextUnreadIds[id] = list[i].id; } }); if (nextUnreadIds[0] !== null && nextUnreadIds[1] !== null) { break; } } }; populateNextUnreadIds(nextUnreadIds, fromIndex, list);
_.findLastIndex
const nextUnreadIds = { [0]: null, [1]: null, } const populateNextUnreadIds = (nextUnreadIds, fromIndex, list) => { const firstIndex = _.findLastIndex( list, (id) => [0, 1].includes(id), fromIndex ); const firstId = list[firstIndex]; nextUnreadIds[firstId] = firstIndex; const secondIdToFind = firstId === 0 ? 1 : 0; const secondIndex = _.findLastIndex( list, (id) => secondIdToFind === id, firstIndex - 1 ); const secondId = list[secondIndex]; nextUnreadIds[secondId] = secondIndex; }; populateNextUnreadIds(nextUnreadIds, fromIndex, list);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for loop
_.findLastIndex
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 break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark is designed to test two different approaches for populating an object with unread IDs from a list: 1. **For Loop**: Using a traditional `for` loop to iterate through the list and populate the object. 2. **Lodash's findLastIndex**: Utilizing Lodash's `findLastIndex` function to achieve the same result. **What's being tested** The benchmark is comparing the performance of these two approaches: * The first test case uses a `for` loop to iterate through the list and populate the object. * The second test case uses Lodash's `findLastIndex` function to find the last index in the list where a specific condition is met (in this case, finding the last occurrence of an ID that exists in the `[0, 1]` array). **Options Compared** The two options being compared are: * **Traditional For Loop**: A simple, iterative approach using a `for` loop to iterate through the list. * **Lodash's findLastIndex**: A more concise and potentially faster approach utilizing Lodash's optimized function. **Pros and Cons of Each Approach** **For Loop:** Pros: * Easy to understand and implement * No additional dependencies required Cons: * Can be slower due to the overhead of loop iterations * May not be as efficient for large datasets **Lodash's findLastIndex:** Pros: * More concise and expressive code * Potential performance benefits due to Lodash's optimized implementation Cons: * Requires an external dependency (Lodash) * May have additional overhead due to function calls and object lookups **Other Considerations** In addition to the two tested approaches, other considerations include: * **List Size**: The benchmark is run with a list of 100,000 elements. This may affect the performance of both approaches. * **Device Platform**: The benchmark is run on a Mac OS X 10.15.7 machine with Chrome 106. This may impact the results due to differences in browser and operating system implementations. **Library and Special JS Features** Lodash is used as a library in the second test case, specifically for its `findLastIndex` function. There are no special JavaScript features mentioned in this benchmark.
Related benchmarks:
findIndex performance
v晚饭23fwefw
Array.includes vs Array.indexOf vs Lodash _.find
indexOf vs Lodash indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?