<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', ahey: 'you', bhey: 'you', chey: 'you', dhey: 'you', ehey: 'you', fhey: 'you', ghey: 'you' } }
const objClone = _.cloneDeep(window.obj);
const objClone = {window.obj};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash deep clone | |
Spread |
Test name | Executions per second |
---|---|
Lodash deep clone | 132304.2 Ops/sec |
Spread | 1684362.9 Ops/sec |
Let's dive into the Benchmark Definition and explore what is being tested, the options compared, their pros and cons, and other considerations.
Benchmark Definition:
The benchmark measures the performance difference between two methods of cloning an object:
_.cloneDeep()
function from the Lodash library to create a deep copy of the original object.{...obj}
to create a shallow copy of the original object.Options Compared:
Pros and Cons:
Library:
In this benchmark, Lodash is used as a third-party library to provide the _.cloneDeep()
function. The library's purpose is to offer a utility function that can be used to create deep copies of objects and arrays in JavaScript.
Special JS Feature/Syntax:
There are no special JavaScript features or syntaxes being tested in this benchmark. However, it's worth noting that the spread clone method relies on the ...
operator, which was introduced in ECMAScript 2018 (ES2018).
Other Alternatives:
If you're interested in exploring alternative methods for cloning objects, here are a few options:
Array.prototype.slice()
to create a shallow copy of the array.Keep in mind that these alternatives may have different performance characteristics or behavior compared to the Lodash deep clone and spread clone methods.