Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Tom test 2
(version: 0)
Compares different approaches to set an object property
Comparing performance of:
Lodash.set vs Dot notation property setter
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>
Script Preparation code:
var person = { name: 'Foo', lastName: 'Bar' };
Tests:
Lodash.set
_.set(person, 'metadata.age', 10)
Dot notation property setter
if (!person.metadata) { person.metadata = {}; } person.metadata.age = 10
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash.set
Dot notation property setter
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, compared, and their pros and cons. **Benchmark Definition** The benchmark compares two approaches to set an object property: using the `_.set` function from Lodash library (option 1) and the dot notation with a conditional check (option 2). **Option 1: Using _.set from Lodash library** * Purpose: The `_set` function is designed to provide a fast and efficient way to set properties on objects. * Library: Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, and object manipulation. **Pros:** 1. **Efficiency**: _.set is optimized for performance and can be faster than other approaches. 2. **Conciseness**: The code is more concise, making it easier to read and maintain. **Cons:** 1. **Dependence on Lodash library**: The benchmark relies on the presence of the Lodash library in the JavaScript environment. 2. **Added overhead**: Including an external library can introduce additional overhead due to parsing and loading time. **Option 2: Dot notation with a conditional check** * Purpose: This approach uses the dot notation (`person.metadata.age = 10`) followed by a conditional check (`if (!person.metadata) { person.metadata = {}; }`) to create the `metadata` property if it doesn't exist. * No specific library is required for this approach. **Pros:** 1. **Self-contained**: This approach does not rely on any external libraries, making it more portable and compatible with all JavaScript environments. 2. **No additional overhead**: There's no need to load an external library, which reduces the overhead associated with parsing and loading time. **Cons:** 1. **Code complexity**: The conditional check can make the code slightly longer and harder to read. 2. **Potential performance impact**: Creating an empty object using `person.metadata = {}` can be slower than assigning properties directly. **Special JS feature or syntax** There is no special JavaScript feature or syntax being tested in this benchmark. Both approaches use standard JavaScript features and do not utilize any advanced ES6+ features, WebAssembly, or other experimental APIs. **Alternatives** Other alternatives for setting object properties include: * Using the `Object.defineProperty()` method to set a property descriptor. * Utilizing modern JavaScript features like destructuring assignment or spread operators (e.g., `{ ...person, metadata: { age: 10 } }`). * Leveraging libraries like Moment.js for date-related properties. However, these alternatives are not being tested in this specific benchmark.
Related benchmarks:
Dot property set notation VS Lodash.set poop
property assign VS Lodash.set
Dot property set notation VS Lodash.set (fix)
Tom test
Lodash.set VS property assign
Comments
Confirm delete:
Do you really want to delete benchmark?