<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
<script type="module">
import fastCopy from 'https://cdn.skypack.dev/fast-copy';
window.fastCopy = fastCopy;
</script>
var MyObject = [
{
"_id": "66e7eff9116f8e690a42def7",
"index": 0,
"guid": "daad7d80-1097-44c6-b914-bab27c62a254",
"isActive": true,
"balance": "$2,505.86",
"picture": "http://placehold.it/32x32",
"age": 30,
"eyeColor": "green",
"name": "Browning Shields",
"gender": "male",
"company": "BUZZNESS",
"email": "browningshields@buzzness.com",
"phone": "+1 (907) 513-2282",
"address": "626 Seaview Court, Trexlertown, Ohio, 6495",
"about": "Incididunt quis ea ipsum elit reprehenderit minim sunt. Do laboris minim voluptate mollit labore non ea amet ipsum sint culpa sunt sunt laboris. Id cillum amet laborum reprehenderit commodo reprehenderit in nulla proident. Voluptate non irure ex velit ex exercitation amet eiusmod magna nostrud do ea ut. Mollit duis ad ipsum Lorem ex enim in adipisicing exercitation ea. Lorem laborum magna est esse anim voluptate dolor aliquip voluptate voluptate duis commodo quis proident. Amet duis irure elit officia.\r\n",
"registered": "2021-11-01T04:00:26 -01:00",
"latitude": -84.115826,
"longitude": 125.358871,
"tags": [
"veniam",
"nostrud",
"eu",
"qui",
"eu",
"commodo",
"ullamco"
],
"friends": [
{
"id": 0,
"name": "Dona Watts"
},
{
"id": 1,
"name": "Mae Cantu"
},
{
"id": 2,
"name": "Dillon Fry"
}
],
"greeting": "Hello, Browning Shields! You have 10 unread messages.",
"favoriteFruit": "banana"
},
{
"_id": "66e7eff92b6ca402ad7ab8bd",
"index": 1,
"guid": "8b628d39-9fff-4d28-b4c5-35bc003a88cc",
"isActive": true,
"balance": "$2,299.78",
"picture": "http://placehold.it/32x32",
"age": 20,
"eyeColor": "blue",
"name": "Dunlap Hubbard",
"gender": "male",
"company": "IRACK",
"email": "dunlaphubbard@irack.com",
"phone": "+1 (938) 440-3887",
"address": "514 Fulton Street, Oasis, Guam, 2302",
"about": "Aute veniam irure est laboris consequat nostrud tempor culpa aliqua ex in exercitation sint ipsum. Ipsum culpa magna est laboris consequat ipsum consequat ullamco fugiat elit. In voluptate ex fugiat consequat in ipsum proident reprehenderit nostrud irure ea nulla esse ex. Velit adipisicing cupidatat cillum aute sint occaecat. Amet elit elit voluptate occaecat elit in adipisicing minim id veniam. Do occaecat laborum dolore pariatur enim deserunt ex reprehenderit in irure.\r\n",
"registered": "2021-10-18T03:34:17 -02:00",
"latitude": 26.597269,
"longitude": -93.956704,
"tags": [
"nulla",
"cillum",
"reprehenderit",
"Lorem",
"laboris",
"consequat",
"non"
],
"friends": [
{
"id": 0,
"name": "Mccoy Kent"
},
{
"id": 1,
"name": "Josefina Carlson"
},
{
"id": 2,
"name": "Angeline Soto"
}
],
"greeting": "Hello, Dunlap Hubbard! You have 6 unread messages.",
"favoriteFruit": "strawberry"
},
{
"_id": "66e7eff9de2dc4dd45378184",
"index": 2,
"guid": "ac90092e-c051-4aeb-9475-f8e0dd72d796",
"isActive": false,
"balance": "$1,097.89",
"picture": "http://placehold.it/32x32",
"age": 34,
"eyeColor": "brown",
"name": "Sharpe Salazar",
"gender": "male",
"company": "EXOSWITCH",
"email": "sharpesalazar@exoswitch.com",
"phone": "+1 (999) 510-2988",
"address": "425 Etna Street, Noblestown, Nevada, 1156",
"about": "Nisi ex mollit do anim reprehenderit quis ad. Labore culpa occaecat Lorem elit labore. Qui aute tempor duis excepteur ea enim est eu voluptate non proident cupidatat enim nisi. Commodo ut non ullamco fugiat qui elit aliqua ipsum cillum occaecat amet. Excepteur nulla dolor sint culpa laborum cupidatat non sunt.\r\n",
"registered": "2017-01-24T11:40:29 -01:00",
"latitude": -84.888941,
"longitude": 148.555312,
"tags": [
"nostrud",
"fugiat",
"aliqua",
"officia",
"culpa",
"cupidatat",
"excepteur"
],
"friends": [
{
"id": 0,
"name": "Dorthy Cameron"
},
{
"id": 1,
"name": "Geneva Adkins"
},
{
"id": 2,
"name": "Chambers Mcneil"
}
],
"greeting": "Hello, Sharpe Salazar! You have 8 unread messages.",
"favoriteFruit": "apple"
},
{
"_id": "66e7eff9f81fd8fa54937393",
"index": 3,
"guid": "e11a6b70-4b2c-4403-a325-f0962b93d8d3",
"isActive": true,
"balance": "$3,010.86",
"picture": "http://placehold.it/32x32",
"age": 31,
"eyeColor": "green",
"name": "Roberta Hayden",
"gender": "female",
"company": "DIGIFAD",
"email": "robertahayden@digifad.com",
"phone": "+1 (910) 446-3209",
"address": "874 Leonora Court, Omar, Nebraska, 1799",
"about": "Ex quis do sunt duis eu aliqua deserunt. Sint cillum amet cupidatat aliqua sunt amet enim. Ut laboris ipsum mollit exercitation velit consequat commodo amet amet mollit. Cupidatat pariatur Lorem Lorem fugiat enim do. Cupidatat ut aliquip dolore consectetur eiusmod in sint. Ad labore culpa consequat non exercitation dolore ut sit qui sunt nostrud voluptate. Nisi eiusmod eu commodo in irure cupidatat culpa cupidatat aliquip et consequat.\r\n",
"registered": "2020-09-18T04:47:32 -02:00",
"latitude": 16.103599,
"longitude": -26.524586,
"tags": [
"aliquip",
"anim",
"laboris",
"commodo",
"ipsum",
"Lorem",
"nisi"
],
"friends": [
{
"id": 0,
"name": "Lela Holder"
},
{
"id": 1,
"name": "Robinson Carney"
},
{
"id": 2,
"name": "Mcintosh Clayton"
}
],
"greeting": "Hello, Roberta Hayden! You have 2 unread messages.",
"favoriteFruit": "banana"
},
{
"_id": "66e7eff9833dea3a5ca1d935",
"index": 4,
"guid": "8633a0f8-51c7-4019-bb15-d9399b4de8c0",
"isActive": true,
"balance": "$3,574.84",
"picture": "http://placehold.it/32x32",
"age": 24,
"eyeColor": "green",
"name": "Fields Mcpherson",
"gender": "male",
"company": "HAIRPORT",
"email": "fieldsmcpherson@hairport.com",
"phone": "+1 (822) 569-2216",
"address": "326 Chestnut Avenue, Singer, New Jersey, 3427",
"about": "Quis anim aliqua ullamco duis. Reprehenderit mollit nulla mollit officia ullamco sunt. Eu sunt proident commodo non. Non dolore proident culpa anim nostrud occaecat magna aute qui eiusmod sunt proident. Pariatur incididunt ipsum cillum incididunt proident minim reprehenderit officia.\r\n",
"registered": "2018-09-02T09:23:44 -02:00",
"latitude": 52.577334,
"longitude": -41.082569,
"tags": [
"laboris",
"ea",
"cillum",
"quis",
"cillum",
"aliquip",
"consectetur"
],
"friends": [
{
"id": 0,
"name": "Santos Higgins"
},
{
"id": 1,
"name": "Bauer Russo"
},
{
"id": 2,
"name": "Drake Dickerson"
}
],
"greeting": "Hello, Fields Mcpherson! You have 4 unread messages.",
"favoriteFruit": "apple"
}
];
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;
}
function clone(obj) {
const clone = Object.create(Object.getPrototypeOf(obj));
Object.getOwnPropertyNames(obj).forEach(key => {
Object.defineProperty(clone, key, Object.getOwnPropertyDescriptor(obj, key));
});
return clone;
}
function clone(obj) {
let result = {};
result = JSON.parse(JSON.stringify(obj));
return result;
}
myCopy = _.cloneDeep(MyObject);
myCopy = _.clone(MyObject);
myCopy = clone(MyObject);
myCopy = clone(MyObject);
myCopy = fastCopy(MyObject);
myCopy = recursiveDeepCopy(MyObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Lodash clone | |
CloneVanilla | |
CloneObject | |
Fast-copy | |
recursiveDeepCopy |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 51571.9 Ops/sec |
Lodash clone | 25158198.0 Ops/sec |
CloneVanilla | 63932.4 Ops/sec |
CloneObject | 60072.2 Ops/sec |
Fast-copy | 92253.1 Ops/sec |
recursiveDeepCopy | 288519.2 Ops/sec |
Based on the provided code, benchmark results, and individual test cases, I will attempt to answer your question.
It appears that you are comparing the performance of different cloning methods for JavaScript objects:
cloneDeep
clone
clone
)To determine the fastest cloning method, we can analyze the benchmark results.
Top 3 winners:
Based on the provided data, the top 3 fastest cloning methods are:
cloneDeep
: With an average execution speed of approximately 68244.88 executions per second.clone
): With an average execution speed of approximately 62441.20 executions per second.Notable observations:
cloneDeep
is significantly faster than Lodash clone
.Please note that these results are based on a single run and may not reflect your actual use case or environment.