Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
findLastItem
(version: 1)
Comparing performance of:
reverse&find vs filter&pop vs filter&lastIndex
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = ['1', '2', '3', '1', '2', '3', '1', '2', '3'];
Tests:
reverse&find
const foundling = arr.reverse().find(item => item === '2');
filter&pop
const foundling = arr.filter(item => item === '2').pop();
filter&lastIndex
const filteredArr = arr.filter(item => item === '2'); const foundling = filteredArr[filteredArr.length - 1];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
reverse&find
filter&pop
filter&lastIndex
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 JSON and explain what's being tested. **Benchmark Definition** The provided JSON defines a benchmark named "findLastItem". The script preparation code creates an array `arr` with some sample data, and there are no additional HTML preparation codes specified. **Script Preparation Code** ```javascript var arr = ['1', '2', '3', '1', '2', '3', '1', '2', '3']; ``` This code defines an array `arr` containing some numbers with duplicates. The purpose of this array is likely to provide a baseline for the benchmark. **Individual Test Cases** The benchmark includes three individual test cases, each representing a different approach to find the last occurrence of a specific item in the array: 1. **reverse&find** ```javascript const foundling = arr.reverse().find(item => item === '2'); ``` This approach reverses the entire array and then uses the `find` method to locate the first occurrence of the item `'2'`. 2. **filter&pop** ```javascript const foundling = arr.filter(item => item === '2').pop(); ``` This approach filters the original array to only include items equal to `'2'`, and then uses the `pop` method to remove and return the last element of the filtered array. 3. **filter&lastIndex** ```javascript const filteredArr = arr.filter(item => item === '2'); const foundling = filteredArr[filteredArr.length - 1]; ``` This approach filters the original array, just like in the second test case, but then uses indexing (`[filteredArr.length - 1]`) to access and return the last element of the filtered array. **Library and Special JS Features** There are no libraries used in this benchmark. However, there is a special JavaScript feature being used: `const` declaration with type annotations (e.g., `item => item === '2'`). This syntax is part of modern JavaScript features introduced in ECMAScript 2015 (ES6). **Pros and Cons** Each approach has its pros and cons: 1. **reverse&find**: Pros - simple to implement, fast performance. Cons - reverses the entire array, which can be inefficient for large arrays. 2. **filter&pop**: Pros - efficient use of `filter` method, easy to understand. Cons - creates a new filtered array, which can consume extra memory. 3. **filter&lastIndex**: Pros - avoids creating a new filtered array, uses indexing, but may be slower due to the additional calculation. **Other Alternatives** Some alternative approaches to find the last occurrence of an item in an array include: * Using `indexOf` method with negative index values (e.g., `-1 + arr.length`) * Using a loop with a counter variable * Using a library function like Lodash's `lastIndexOf` Keep in mind that these alternatives may not be as efficient or readable as the approaches tested in this benchmark.
Related benchmarks:
lodash flatmap long
`Array.slice(0, N)` vs `Array.length = N` sd4343
`Array.slice(0, N)` vs `Array.length = N` sd434345345
`Array.slice(0, N)` vs `Array.length = N` sd434332432
Array length assertion
Comments
Confirm delete:
Do you really want to delete benchmark?