Created
July 17, 2018 10:18
-
-
Save re-gor/c5773ca7837d34b5bf82aff380795f4a to your computer and use it in GitHub Desktop.
deepCopyTest.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {cloneDeep} = require('lodash'); | |
const sizeof = require('object-sizeof'); | |
function cloneWeird(obj) { | |
return JSON.parse(JSON.stringify(obj)); | |
} | |
function generateString() { | |
return Math.random().toString(36).slice(2); | |
} | |
function createObject(depth=0, wide=5) { | |
const obj = Object.create(null); | |
for (let i = 0; i < wide; ++i) { | |
obj[generateString()] = depth ? createObject(depth - 1, wide) : Math.random() | |
} | |
return obj; | |
} | |
const REPEATS = 1e2 | |
function test(cb, depth, wide) { | |
let obj = createObject(depth, wide); | |
let time = 0; | |
for (let i = 0; i < REPEATS; ++i) { | |
let start = new Date(); | |
obj = cb(obj); | |
let end = new Date(); | |
time += end - start | |
} | |
return [time / REPEATS, time]; | |
} | |
for (let depth = 1, i = 1; depth < 6; depth += 1, ++i) { | |
const _depth = depth - 1; | |
const obj = createObject(_depth, wide=20); | |
console.group(`Round ${i}: size: ${sizeof(obj)} B`) | |
console.log(`Method \tspeed (ms/iter)\t time (s)`) | |
let [speed, all] = test(cloneDeep, _depth); | |
console.log(`Lodash:\t${speed} ms/iter\t ${all / 1000} s`) | |
let [speed2, all2] = test(cloneWeird, _depth); | |
console.log(` JSON:\t${speed2} ms/iter\t ${all2 / 1000} s`) | |
console.groupEnd(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment