Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs _.defaultsDeep
(version: 0)
Comparing performance of:
Object.assign vs _.defaults
Created:
5 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>
Script Preparation code:
var schema = { name: undefined, password: undefined, photo: undefined, role: 1, level: 1, resPass: false, stories: { one: 'Hola mundo', two: false, three: false } } var user = { name: "Jacob", password: 123, photo: "img.jpg", stories: { two: "Adios mundo" } }
Tests:
Object.assign
let objectAssign = Object.assign({}, schema, user); objectAssign.stories = Object.assign(schema.stories, user.stories); console.log(objectAssign);
_.defaults
const defaultsDeep = _.defaultsDeep(schema, user); console.log(defaultsDeep);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.assign
_.defaults
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.assign
409639.2 Ops/sec
_.defaults
370385.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **Overview** The benchmark compares two JavaScript functions: `Object.assign` and `_defaultDeep` (a function from the Lodash library). The goal is to measure which function is faster when used to merge objects. **Options Compared** There are two options being compared: 1. **Object.assign**: A built-in JavaScript method that merges all own enumerable properties of source objects into a target object. 2. **_.defaultsDeep` (from Lodash): A function that returns a new object with the default values for each key in the schema, merged with the provided user object. **Pros and Cons** * **Object.assign**: + Pros: Built-in JavaScript method, easy to use, and fast. + Cons: Can be slow if the number of properties is large, as it iterates over all properties. * **_.defaultsDeep` (Lodash): + Pros: Optimized for performance, handles nested objects, and can be faster than `Object.assign` for large datasets. + Cons: Requires an external library (Lodash), which may add overhead. **Library Description** The benchmark uses Lodash, a popular JavaScript utility library. `_defaultDeep` is a function that recursively merges two objects by setting default values for missing keys. In this case, it's used to merge the `schema` object with the `user` object. **Special JS Features or Syntax** There are no special JavaScript features or syntax being tested in this benchmark. **Other Alternatives** If you don't want to use Lodash, you can implement a similar function using JavaScript: ```javascript function defaultDeep(schema, user) { const result = {}; for (const key in schema) { if (!user[key]) { result[key] = schema[key]; } else if (typeof schema[key] === 'object' && typeof user[key] === 'object') { result[key] = defaultDeep(schema[key], user[key]); } } return result; } ``` However, this implementation might not be as optimized or efficient as the Lodash version. **Benchmark Preparation** The benchmark prepares two objects: `schema` and `user`. The `schema` object has some properties with default values (e.g., `role`, `level`) and some properties that are undefined (e.g., `name`, `password`). The `user` object has some properties that are defined (e.g., `photo`) and some properties that are nested objects. The benchmark then defines two test cases: 1. Using `Object.assign` to merge the `schema` object with the `user` object, setting `stories` recursively. 2. Using `_defaultsDeep` from Lodash to merge the `schema` object with the `user` object. Finally, the benchmark runs both tests and measures their execution times using the Chrome browser on a Mac OS X system.
Related benchmarks:
Object.assign vs Lodash.assign
Checks if value is an object
lodash assign vs native
ramdajs vs lodash assign vs native
Lodash.assign vs Object.assign
Comments
Confirm delete:
Do you really want to delete benchmark?