Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash _.last vs native
(version: 1)
Comparing performance of:
lodash vs native
Created:
5 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 = [1,2,3,4,5,6,7,8,9]; var arr2 = []; var arr3 = undefined; var arr4 = ['asdsa', 3, undefined, 32, 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8];
Tests:
lodash
_.last(arr1) _.last(arr2) _.last(arr3) _.last(arr4)
native
arr1?.reverse()?.[0] arr2?.reverse()?.[0] arr3?.reverse()?.[0] arr4?.reverse()?.[0]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash
native
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 benchmark and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches to get the last element of an array: 1. Using the `_last` function from Lodash, a popular utility library for JavaScript. 2. Using the `?.reverse()?.[0]` syntax, which is a shorthand way to reverse the array and then access the first element. **Options Compared** The two options being compared are: * **Lodash**: The `_last` function is part of the Lodash library, which provides a set of high-order functions for working with arrays, objects, and other data structures. + Pros: - Well-maintained and widely used library - Provides additional functionality beyond just getting the last element + Cons: - Adds dependency on an external library - May introduce overhead due to the size of the library * **Native**: The `?.reverse()?.[0]` syntax is a shorthand way to reverse the array and then access the first element using only built-in JavaScript functionality. + Pros: - No additional dependencies or overhead - More efficient, as it uses native functions + Cons: - Less readable and maintainable for complex operations - May not work in older browsers or environments that don't support this syntax **Library** The Lodash library is a popular utility library that provides a set of high-order functions for working with arrays, objects, and other data structures. It's widely used in JavaScript development due to its simplicity and flexibility. **Special JS Feature/Syntax** There isn't any special JavaScript feature or syntax being tested in this benchmark, aside from the `?.` syntax which is called the "optional chaining" operator. This operator was introduced in ECMAScript 2020 (ES12) as a way to safely navigate nested properties without throwing errors. **Other Considerations** When choosing between these two approaches, consider the trade-offs: * If you need more functionality beyond just getting the last element of an array, Lodash might be a better choice. * If you prioritize performance and efficiency, using native functions like `?.reverse()?.[0]` might be a better option. **Alternatives** Other alternatives for getting the last element of an array include: * Using the `slice()` method to create a new array with the last element: `arr.slice(-1)` * Using the `at()` method (available in modern browsers): `arr.at(-1)`
Related benchmarks:
native vs lodash
_.isEmpty vs !array || !array.length
Spread Operator vs Lodash with not so many items
lodash size vs simple if
Lodash flatten & map vs native js
Comments
Confirm delete:
Do you really want to delete benchmark?