Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native find vs lodash _.get & _.find
(version: 0)
Lodash vs Native
Comparing performance of:
native vs lodash
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>
Script Preparation code:
var data = { tags: [{ 'category': 'Rarity', 'name': 'Covert', }, { 'category': 'Exterior', 'name': 'Factory New', }, { 'user': 'Collection', 'name': 'Test Collection', } ] }
Tests:
native
// Native data.tags ?.find((x) => x?.category === 'Exterior') ?.name ?? null;
lodash
_.get( _.find(data.tags, { category: 'Exterior' }), 'name', null, );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
native
lodash
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 definition and test cases. **Benchmark Definition:** MeasureThat.net is testing two approaches to find an element in an array using JavaScript: 1. Native (JavaScript built-in method) 2. Lodash _.get & _.find (Lodash library) The benchmarking script creates a sample data object with an array of objects, where each object has a "category" and "name" property. The goal is to find the element in the array that matches a specific category ("Exterior") and extract its "name" value. **Options Compared:** Two options are being compared: 1. Native (JavaScript built-in method): This approach uses the optional chaining operator (`?.`) followed by the nullish coalescing operator (`??`) to find the desired element. 2. Lodash _.get & _.find (Lodash library): This approach uses two separate functions from the Lodash library: * `_.find(data.tags, { category: 'Exterior' })`: finds the first element in the array that matches the specified condition. * `_.get(..., 'name', null)`: extracts the "name" value from the found element. **Pros and Cons of Each Approach:** 1. Native (JavaScript built-in method): * Pros: + Faster execution time since it doesn't require an additional library. + More lightweight and concise code. * Cons: + May not be as readable or maintainable, especially for complex conditions. + Might have limitations in terms of support for older browsers or environments. 2. Lodash _.get & _.find (Lodash library): * Pros: + More readable and maintainable code, thanks to the explicit separation of concerns between finding the element and extracting its value. + Can be more flexible and adaptable to different use cases. * Cons: + Requires an additional library, which may incur a small performance overhead. + Code might be slightly longer and less concise. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks like array manipulation, object transformation, and more. In this benchmark, the `_.get` and `_.find` functions are used to simplify the code and improve readability. The `_.get` function takes three arguments: the value to extract from, the key path, and a default value to return if the key is not present. **Special JS Feature/ Syntax:** None of the benchmarking scripts use any special JavaScript features or syntax that would be unfamiliar to most software engineers. **Other Alternatives:** If you don't want to use Lodash, there are alternative approaches: 1. Vanilla JavaScript: * Use `Array.prototype.find()` instead of `_.find()`. * Implement the same logic manually without using the optional chaining operator (`?.`) or nullish coalescing operator (`??`). 2. Other libraries: * You could use other array manipulation libraries like Ramda, Underscore.js, or even Svelte's built-in functions. Keep in mind that while alternative approaches exist, Lodash provides a convenient and readable way to perform common tasks, making it a popular choice among developers.
Related benchmarks:
native find vs lodash _.find..
Array find vs lodash _.find
native find vs lodash _.find equal
native find vs lodash _.find_fork
native find vs lodash _.find for objects equality
Comments
Confirm delete:
Do you really want to delete benchmark?