<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
window.obj = { foo: 'some string', bar: 'another', subObj: { hey: 'you', what: 'are you doing' } }
const objClone = _.cloneDeep(window.obj);
const objClone = { window.obj, subObj: { window.obj.subObj }};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash deep clone | |
Spread |
Test name | Executions per second |
---|---|
Lodash deep clone | 1309861.1 Ops/sec |
Spread | 5012740.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares two approaches to cloning an object in JavaScript:
_.cloneDeep
: This method is part of the Lodash library, which provides a set of utility functions for functional programming. _.cloneDeep
creates a deep clone of an object, meaning it recursively copies all properties and values from the original object.{ ...window.obj }
): This approach uses the spread operator (...
) to create a new object that includes all properties from the original window.obj
.Options Compared
The benchmark compares two options:
_.cloneDeep
: A library-based approach that provides a robust and efficient way to clone objects.Pros and Cons
Lodash _.cloneDeep
Pros:
Cons:
Spread Cloning ({ ...window.obj }
)
Pros:
Cons:
_.cloneDeep
due to the need to manually create new objects and copy properties.Other Considerations
The benchmark does not consider other approaches, such as using Object.assign()
or JSON.parse(JSON.stringify())
, which may also be viable options for cloning objects in JavaScript.
Library: Lodash
Lodash is a popular utility library for functional programming in JavaScript. It provides a wide range of functions for tasks such as array manipulation, object cloning, and more. In this benchmark, Lodash's _.cloneDeep
function is used to create a deep clone of the window.obj
object.
Special JS Feature/Syntax: None
There are no special JavaScript features or syntax used in this benchmark, making it accessible to developers with varying levels of experience with the language.