Skip to content

Instantly share code, notes, and snippets.

@Aschen
Created March 16, 2022 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aschen/bce8195e81f220c6e66b360505e62fc6 to your computer and use it in GitHub Desktop.
Save Aschen/bce8195e81f220c6e66b360505e62fc6 to your computer and use it in GitHub Desktop.
Cloning Javascript Objects
const {Benchmark} = require('benchmark');
var suite = new Benchmark.Suite;
const justClone = require('just-clone');
const fastClone = require('fast-clone');
const lodashClone = require('lodash').cloneDeep;
const stringifyParse = o => JSON.parse(JSON.stringify(o));
const smallObject = {
name: 'Aschen',
age: 28,
cities: ['minsk', 'tbilisi', 'colombo', 'pondicherry'],
skills: {
programming: ['js', 'ruby', 'c++'],
design: ['photoshop', 'illustrator', 'indesign']
}
};
const largeObject = {
"_id": "62315fcee4eaff97dceabe31",
"index": 0,
"guid": "7b2b17a4-3ba0-4795-8489-8a87862e6c11",
"isActive": true,
"balance": "$1,558.92",
"picture": "http://placehold.it/32x32",
"age": 40,
"eyeColor": "blue",
"name": "Shannon Joyce",
"gender": "male",
"company": "MOMENTIA",
"email": "shannonjoyce@momentia.com",
"phone": "+1 (934) 452-2162",
"address": "214 Schweikerts Walk, Groveville, Rhode Island, 278",
"about": "Mollit cillum consequat tempor culpa consequat commodo culpa in in reprehenderit. Cupidatat voluptate cupidatat eiusmod eiusmod duis occaecat ullamco voluptate mollit enim. Officia ea tempor dolor pariatur est pariatur anim qui dolore officia dolor nostrud nulla. Velit aliquip esse ea aliqua sunt.\r\n",
"registered": "2017-08-21T01:00:00 -02:00",
"latitude": 71.932758,
"longitude": 138.604389,
"tags": [
"exercitation",
"tempor",
"ullamco",
"excepteur",
"irure",
"tempor",
"officia"
],
"friends": [
{
"id": 0,
"name": "Francesca Patterson"
},
{
"id": 1,
"name": "Casandra Hoffman"
},
{
"id": 2,
"name": "Rose Mccray"
}
],
"greeting": "Hello, Shannon Joyce! You have 6 unread messages.",
"favoriteFruit": "banana",
"nested": {
"_id": "62315fceae0d16aacc9e9c1a",
"index": 1,
"guid": "38d58b39-69ef-420c-8497-b9c2138db380",
"isActive": false,
"balance": "$2,641.57",
"picture": "http://placehold.it/32x32",
"age": 26,
"eyeColor": "blue",
"name": "Sadie Richmond",
"gender": "female",
"company": "SENTIA",
"email": "sadierichmond@sentia.com",
"phone": "+1 (970) 413-3664",
"address": "299 Murdock Court, Gallina, American Samoa, 5729",
"about": "Officia elit elit mollit sunt eiusmod sit Lorem deserunt ut duis ea dolore. Sint elit dolor magna veniam excepteur dolore incididunt qui quis proident cillum duis Lorem. Proident incididunt cillum dolore dolore incididunt esse adipisicing excepteur pariatur veniam. Sunt consequat cupidatat amet ut non. Eu cupidatat laborum ex deserunt ad. Ad eiusmod minim ut cupidatat sunt esse.\r\n",
"registered": "2017-02-12T03:58:56 -01:00",
"latitude": 20.921124,
"longitude": -159.652856,
"tags": [
"id",
"laboris",
"enim",
"nisi",
"labore",
"deserunt",
"veniam"
],
"friends": [
{
"id": 0,
"name": "Baird Miranda"
},
{
"id": 1,
"name": "Lois York"
},
{
"id": 2,
"name": "Nannie Wagner"
}
],
"greeting": "Hello, Sadie Richmond! You have 6 unread messages.",
"favoriteFruit": "apple"
}
};
let ret;
// add tests
suite
.add('smallObject just-clone', function() {
ret = justClone(smallObject);
})
.add('smallObject fast-clone', function() {
ret = fastClone(smallObject);
})
.add('smallObject lodash.cloneDeep', function() {
ret = lodashClone(smallObject);
})
.add('smallObject JSON.parse + JSON.stringify', function() {
ret = stringifyParse(smallObject);
})
.add('largeObject just-clone', function() {
ret = justClone(largeObject);
})
.add('largeObject fast-clone', function() {
ret = fastClone(largeObject);
})
.add('largeObject lodash.cloneDeep', function() {
ret = lodashClone(largeObject);
})
.add('largeObject JSON.parse + JSON.stringify', function() {
ret = stringifyParse(largeObject);
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run();
smallObject just-clone x 313,493 ops/sec ±3.39% (90 runs sampled)
smallObject fast-clone x 236,720 ops/sec ±0.81% (95 runs sampled)
smallObject lodash.cloneDeep x 217,750 ops/sec ±4.14% (89 runs sampled)
smallObject JSON.parse + JSON.stringify x 268,393 ops/sec ±1.87% (94 runs sampled)
largeObject just-clone x 72,248 ops/sec ±1.38% (92 runs sampled)
largeObject fast-clone x 41,653 ops/sec ±3.18% (86 runs sampled)
largeObject lodash.cloneDeep x 42,870 ops/sec ±0.71% (93 runs sampled)
largeObject JSON.parse + JSON.stringify x 49,257 ops/sec ±0.71% (91 runs sampled)

Fastest is smallObject just-clone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment