HTML Preparation code:
AخA
 
1
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
2
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Tests:
  • JSON.parse(JSON.stringify())

     
    const base = {
        firstName: 'John',
      lastName: 'Doe',
      size: 170,
      birthDate: new Date(1985, 0,15),
      married: true,
      address: {
        street: '2 av Victor Hugo',
        country: 'FR',
      },
      childrens: [
        {
            name: 'Paul',
          age: 8,
        },
      ],
      functions: [
        function () { return 'Base function' },
      ],
    };
    const copy = JSON.parse(JSON.stringify(base));
  • Custom function

    x
     
    function clone(aObject) {
      // Prevent undefined objects
      // if (!aObject) return aObject;
      let bObject = Array.isArray(aObject) ? [] : {};
      let value;
      for (const key in aObject) {
        // Prevent self-references to parent object
        // if (Object.is(aObject[key], aObject)) continue;
        value = aObject[key];
        bObject[key] = (typeof value === "object") ? clone(value) : value;
      }
      return bObject;
    };
    // base object
    const base = {
        firstName: 'John',
      lastName: 'Doe',
      size: 170,
      birthDate: new Date(1985, 0,15),
      married: true,
      address: {
        street: '2 av Victor Hugo',
        country: 'FR',
      },
      childrens: [
        {
            name: 'Paul',
          age: 8,
        },
      ],
      functions: [
        function () { return 'Base function' },
      ],
    };
    const copy = clone(base);
  • Lodash.cloneDeep()

     
    const base = {
        firstName: 'John',
      lastName: 'Doe',
      size: 170,
      birthDate: new Date(1985, 0,15),
      married: true,
      address: {
        street: '2 av Victor Hugo',
        country: 'FR',
      },
      childrens: [
        {
            name: 'Paul',
          age: 8,
        },
      ],
      functions: [
        function () { return 'Base function' },
      ],
    };
    const copy = _.cloneDeep(base);
  • jQuery.extend(true)

     
    const base = {
        firstName: 'John',
      lastName: 'Doe',
      size: 170,
      birthDate: new Date(1985, 0,15),
      married: true,
      address: {
        street: '2 av Victor Hugo',
        country: 'FR',
      },
      childrens: [
        {
            name: 'Paul',
          age: 8,
        },
      ],
      functions: [
        function () { return 'Base function' },
      ],
    };
    const copy = jQuery.extend(true, {}, base);
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    JSON.parse(JSON.stringify())
    Custom function
    Lodash.cloneDeep()
    jQuery.extend(true)

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: one year ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50
Chrome 113 on Windows
View result in a separate tab
Test name Executions per second
JSON.parse(JSON.stringify()) 244468.1 Ops/sec
Custom function 509495.2 Ops/sec
Lodash.cloneDeep() 371343.7 Ops/sec
jQuery.extend(true) 410452.3 Ops/sec