Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Dot property set notation VS Lodash.set(nested)
(version: 1)
Compares different approaches to set an object property
Comparing performance of:
Dot notation property setter vs Lodash.set
Created:
one year 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', nested: {nested: {nested: 1}} };
Tests:
Dot notation property setter
_.set(person, 'nested.nested.nested.age', 10)
Lodash.set
person.nested.nested.nested.age = 10
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Dot notation property setter
Lodash.set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Dot notation property setter
5708307.5 Ops/sec
Lodash.set
141391072.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark described compares two different methods of setting properties on a nested object in JavaScript: using dot notation and using the Lodash library's `_.set` function. ### Approaches Compared 1. **Dot Notation Property Setter**: - **Code**: `person.nested.nested.nested.age = 10;` - **Description**: This approach uses the dot notation to directly set the value of `age` on a deeply nested object structure. It is straightforward and performs direct property assignments on the object. 2. **Lodash.set**: - **Code**: `_.set(person, 'nested.nested.nested.age', 10);` - **Description**: This method uses the Lodash library's `set` function, which allows you to define the property path as a string and seamlessly set the intended property, even in cases where some nested properties might not exist initially. ### Pros and Cons #### Dot Notation Property Setter **Pros**: - **Performance**: As evidenced by the benchmark results, this method is significantly faster, executing approximately 5708307.5 operations per second. - **Simplicity**: Standard JavaScript syntax; no additional libraries are required, keeping it lightweight. **Cons**: - **Error-Prone**: If any part of the nested structure doesn’t exist, it will throw an error (e.g., if `nested` or `nested.nested` doesn't exist). - **Readability**: Deeply nested assignments can become hard to read, especially when the hierarchy is complex. #### Lodash.set **Pros**: - **Flexibility**: Can set properties on paths that might not exist yet, creating intermediate objects as necessary. - **Readability**: The string path allows for clearer intention of nesting, which may help with understanding the code. **Cons**: - **Performance Overhead**: The benchmark shows its performance at approximately 141391072 operations per second, indicating that while it’s still fast, it is slower than using dot notation. - **Dependency**: Requires importing the Lodash library, making it slightly heavier on dependencies. ### Other Considerations 1. **Use Cases**: - If performance is paramount and you're sure of the existence of properties, use dot notation. - If dealing with complex objects or where the structure may change, Lodash is a safer option to avoid potential runtime errors. 2. **Alternatives**: - **Native JavaScript Alternatives**: JavaScript objects also support the bracket notation (`person['nested']['nested']['nested']['age'] = 10;`), which allows dynamic property assignment but does not mitigate errors if properties in the chain are missing. - **Proxy or Immutable.js**: These are other libraries or tools which might be considered for more complex state management or to handle immutability but involve more complexity than the direct comparisons here. 3. **Library**: **Lodash** - A popular utility library in JavaScript that provides a range of functions to manipulate arrays, numbers, objects, strings, etc. - Particularly useful for dealing with deep data structures or performing complex operations that may require concise and clear function calls. Overall, the benchmark presents a clear distinction between using native JavaScript methods and employing a utility library, highlighting performance differences while considering the trade-offs in flexibility and reliability.
Related benchmarks:
Dot property set notation VS Lodash.set
Dot property set notation VS Lodash.set poop
property assign VS Lodash.set
Dot property set notation VS Lodash.set (Nested)
Lodash.get vs Property dot notation (Nested)
Dot property set notation VS Lodash.set (fix)
Dot property set notation VS Lodash.set (initial attribute existing 2)
Tom test
Lodash.set VS property assign
Comments
Confirm delete:
Do you really want to delete benchmark?