Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash set vs custom set
(version: 0)
Comparing performance of:
lodash set vs custom set
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script scr="https://cdn.jsdelivr.net/npm/lodash@4.17.10/lodash.min.js"></script>
Script Preparation code:
function set(object, path, value) { if (path in object) { object[path] = value; return; } path = path.split('.'); //extract last item and store it const lastProp = path.pop(); if (path.length) { let obj = object; let prop; for (prop of path) { if (prop in obj) { obj = obj[prop]; } else { return; } } obj[lastProp] = value; } } var testObject = { a: { b: 1 } }
Tests:
lodash set
_.set(testObject, 'a.b', 2)
custom set
set(testObject, 'a.b', 2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
lodash set
custom set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
lodash set
4912472.0 Ops/sec
custom set
12998907.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON. **Benchmark Definition** The benchmark is designed to compare two approaches for setting values on an object: using Lodash's `_.set()` function and creating a custom implementation of the same functionality. **Options Compared** 1. **Lodash's `_set()` function**: This is a built-in utility function in the Lodash library that sets a value on an object at a specified path. 2. **Custom set implementation**: A custom JavaScript function named `set()` that takes an object, a path, and a value as arguments. It uses a recursive approach to traverse the object and set the value. **Pros and Cons** 1. **Lodash's `_set()` function**: * Pros: Fast execution, well-tested and maintained, and part of a larger library with many other useful utility functions. * Cons: Adds extra dependency on Lodash, which may not be desirable in all cases. 2. **Custom set implementation**: * Pros: No additional dependencies, can be optimized for specific use cases, and provides insight into the performance characteristics of such an implementation. * Cons: Requires more boilerplate code and manual maintenance, potentially leading to errors. **Library** Lodash is a popular JavaScript library that provides a collection of useful functions for working with data structures. In this case, `_.set()` is used to set values on objects at specific paths. **JavaScript Feature/Syntax** There's no special JavaScript feature or syntax being tested in this benchmark. The focus is solely on the performance comparison between two implementation approaches. **Other Alternatives** If you want to create a custom set function without using Lodash, you can use other libraries like Underscore.js (which provides similar functionality) or implement your own recursive approach as shown in the custom set implementation. Keep in mind that this benchmark is designed to compare performance between two specific implementations, so if you're interested in exploring alternative approaches, you may want to create your own benchmark or experiment with different techniques.
Related benchmarks:
Dot property set notation VS Lodash.set
Dot property set notation VS Lodash.set poop
Setting of intermediate objects
Dot property set notation VS Lodash.set (fix)
Comments
Confirm delete:
Do you really want to delete benchmark?