Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
Chrome 90
Windows
Desktop
3 years ago
Test name Executions per second
cloneDeep 208237.9 Ops/sec
JSON 184530.5 Ops/sec
DeepCopy 224251.2 Ops/sec
HTML Preparation code:
AخA
 
1
<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>
Script Preparation code:
x
 
var myArray = [{itemType:"text",itemId:"zy5mhESQ",text:"","sound":""},{itemType:"micromodule",itemId:"CczUjEMY",id:57191,scores:{rightAnswer:1000,partialAnswer:500,wrongAnswer:0}},{itemType:"text",itemId:"RSOqEJzl",text:"",sound:""}];
var myCopy = null;
function recursiveDeepCopy(o) {
    var newO,
      i;
  
    if (typeof o !== 'object') {
      return o;
    }
    if (!o) {
      return o;
    }
  
    if ('[object Array]' === Object.prototype.toString.apply(o)) {
      newO = [];
      for (i = 0; i < o.length; i += 1) {
        newO[i] = recursiveDeepCopy(o[i]);
      }
      return newO;
    }
  
    newO = {};
    for (i in o) {
      if (o.hasOwnProperty(i)) {
        newO[i] = recursiveDeepCopy(o[i]);
      }
    }
    return newO;
  }
Tests:
  • cloneDeep

     
    myCopy = _.cloneDeep(myArray);
  • JSON

     
    myCopy = JSON.parse(JSON.stringify(myArray));
  • DeepCopy

     
    myCopy = recursiveDeepCopy(myArray);