Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Find last element of an array
(version: 31)
Find last element of an array
Comparing performance of:
lodash vs length-1 vs slice vs pop
Created:
2 years ago
by:
Registered User
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:
var arr1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; var arr2 = []; function lodashFindLast(arr, n) { const element = _.findLast(arr); return element === n; } function lengthLast(arr, n) { const element = arr[arr.lenght - 1]; return element === n; } function popLast(arr, n) { const element = arr.pop(); return element === n; } function sliceArray(arr, n) { const element = arr.slice(-1); return element === n; }
Tests:
lodash
lodashFindLast(arr1, 8); lodashFindLast(arr1, 9); lodashFindLast(arr2, 9);
length-1
lengthLast(arr1, 8); lengthLast(arr1, 9); lengthLast(arr2, 9);
slice
sliceArray(arr1, 8); sliceArray(arr1, 9); sliceArray(arr2, 9);
pop
popLast(arr1, 8); popLast(arr1, 9); popLast(arr2, 9);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash
length-1
slice
pop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 OPR/119.0.0.0
Browser/OS:
Opera 119 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash
11059048.0 Ops/sec
length-1
27398826.0 Ops/sec
slice
25973702.0 Ops/sec
pop
155954528.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark called "Find last element of an array". The benchmark is defined by three scripts: 1. `lodashFindLast(arr, n)`: This function uses the Lodash library to find the last element of an array that matches the value `n`. 2. `lengthLast(arr, n)`: This function directly accesses the last element of the array using its length property. 3. `popLast(arr, n)`: This function removes and returns the last element from the array, then checks if it's equal to `n`. 4. `sliceArray(arr, n)`: This function creates a new array containing only the last element of the original array. **Options being compared** The benchmark compares the performance of these four approaches: * Using Lodash (`lodashFindLast`) vs. direct indexing (`lengthLast`) * Using direct indexing (`lengthLast`) vs. using `pop()` and comparing (`popLast`) * Using direct indexing (`lengthLast`) vs. slicing the array (`sliceArray`) **Pros and cons** 1. **Lodash (`lodashFindLast`)**: * Pros: Robust and efficient implementation of array methods, including finding the last element. * Cons: Adds an external library dependency, which may not be desirable for some use cases. 2. **Direct indexing (`lengthLast`)**: * Pros: Fast and lightweight implementation that doesn't require any additional libraries. * Cons: May not work correctly if the array is large or if the language itself has issues with direct indexing (e.g., in older browsers). 3. **Using `pop()` and comparing (`popLast`)**: * Pros: Simple and efficient implementation, as it only requires removing and comparing the last element. * Cons: May not be as robust as other approaches, especially if the array is large or frequently modified. 4. **Slicing the array (`sliceArray`)**: * Pros: Works well for small to medium-sized arrays and can be efficient in some cases. * Cons: May not be suitable for large arrays due to its O(n) complexity. **Library usage** The benchmark uses Lodash, a popular utility library for JavaScript. Lodash provides a robust implementation of various array methods, including `findLast`, which is used by the `lodashFindLast` function. **Special JS feature or syntax** There is no special JavaScript feature or syntax being tested in this benchmark. The focus is on comparing different approaches to finding the last element of an array. **Other alternatives** If you'd like to explore alternative solutions, here are a few options: 1. **Using `Array.prototype.indexOf()`**: This approach finds the index of the last element that matches the given value. 2. **Using `Array.prototype.lastIndexOf()`**: This approach finds the index of the last occurrence of the given value in the array. 3. **Using a custom implementation with binary search**: This approach uses a custom binary search algorithm to find the last element, which can be more efficient for large arrays. Keep in mind that these alternatives might not provide better performance or accuracy compared to the benchmark's existing solutions.
Related benchmarks:
Array clone
last item in array [length-1] vs slice(-1) vs pop
`array.slice(-1)[0]` vs `array[array.length - 1]`
pop vs index for array
Last element slice vs pop
Comments
Confirm delete:
Do you really want to delete benchmark?