Lodash cloneDeep vs Object spread

5 months ago
User agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Test name Executions per second
_cloneDeep 43506.0 Ops/sec
Object spread 194373.7 Ops/sec
Structured clone 38711.9 Ops/sec
Object.assign 141444.1 Ops/sec
HTML Preparation code:
x
 
1
2
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
 
window.obj = {};
// 10 number properties
for (let i = 0; i < 10; i++) {
    const key = (Math.random() + 1).toString(36).substring(7);
    obj[key] = Math.random();
}
// 10 nested object properties
for (let i = 0; i < 10; i++) {
    const key = (Math.random() + 1).toString(36).substring(7);
    obj[key] = {
        foo: Math.random(),
        bar: true,
        baz: {
            baz: "Hello World"
        }
    }
}
Tests:
  • _cloneDeep

     
    const obj2 = _.cloneDeep(obj)
  • Object spread

     
    const obj3 = {...obj}
  • Structured clone

     
    const obj4 = structuredClone(obj)
  • Object.assign

     
    const obj5 = Object.assign({}, obj)
Open this result on MeasureThat.net