Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash defaultTo vs native nullish operation
(version: 0)
Comparing performance of:
lodash defaulTo vs native nullish operation
Created:
3 years ago
by:
Guest
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>
Tests:
lodash defaulTo
let obj = {a: 'a', b: null}; const result1 = _.defaultTo(obj.a, 'default'); const result2 = _.defaultTo(obj.b, 'default'); const result3 = _.defaultTo(obj.c, 'default');
native nullish operation
let obj = {a: 'a', b: null}; const result1 = obj.a ?? 'default'; const result2 = obj.b ?? 'default'; const result3 = obj.c ?? 'default';
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash defaulTo
native nullish operation
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash defaulTo
133532864.0 Ops/sec
native nullish operation
148188384.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition:** The benchmark is comparing two approaches for handling null or undefined values in JavaScript: 1. Using Lodash's `defaultTo` function, which returns a default value if the provided value is nullish (either null or undefined). 2. Using the native nullish operation (`??` operator), introduced in ECMAScript 2020. **Options Compared:** The benchmark is comparing the performance of these two approaches: * Lodash's `defaultTo` function * The native nullish operation (`??` operator) **Pros and Cons:** * **Lodash's `defaultTo` function:** + Pros: - Provides a clear and explicit way to handle default values. - Can be customized with different default values for different cases. + Cons: - Adds an extra dependency on the Lodash library, which can increase bundle size and potentially slow down execution. - May have performance overhead due to function call and argument passing. * **Native nullish operation (`??` operator):** + Pros: - Eliminates the need for external libraries or functions, reducing bundle size and potential performance overhead. - Is a native JavaScript feature, so it's already implemented in most modern browsers. + Cons: - Limited to handling null and undefined values; cannot handle other types of default values (e.g., numbers, strings). - May not be supported in older browsers or environments that don't have this operator. **Library:** Lodash is a popular utility library for JavaScript, providing a wide range of functions for tasks like array manipulation, string processing, and more. In this benchmark, Lodash's `defaultTo` function is used to handle default values in a predictable way. **Special JS Feature or Syntax:** The nullish operation (`??`) is a new feature introduced in ECMAScript 2020. It allows for a concise way to handle null and undefined values in expressions. The syntax is simple: `a ?? b` returns the value of `a` if it's not nullish, otherwise returns the value of `b`. **Other Alternatives:** If you need more flexibility in handling default values or want to avoid using external libraries, other alternatives include: * Using a conditional expression (`?:` operator): `a ? a : 'default'` * Creating a custom function or method for handling default values * Using other libraries or frameworks that provide similar functionality (e.g., React's default props) Keep in mind that these alternatives may have their own trade-offs and potential performance implications.
Related benchmarks:
trim loadsh vs native trim
Lodash.js vs Native - empty
Lodash isEmpty vs Native Javascript
lodash noop vs new function
lodash noop vs new function vs optional chaining
Comments
Confirm delete:
Do you really want to delete benchmark?