Created
August 23, 2013 16:51
-
-
Save clarle/6321507 to your computer and use it in GitHub Desktop.
Deep copy performance tests with Benchmark.js
This file contains 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
var Benchmark = require('benchmark'), | |
suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function() { | |
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 jsonDeepCopy(o) { | |
return JSON.parse(JSON.stringify(o)); | |
} | |
var dimensions = [{ | |
"dimensions": [{ | |
"runtime": { | |
"common": { | |
"client": null, | |
"server": null | |
} | |
} | |
}, { | |
"device": { | |
"android": null, | |
"blackberry": null, | |
"iemobile": null, | |
"iphone": null, | |
"ipad": null, | |
"kindle": null, | |
"opera-mini": null, | |
"palm": null | |
} | |
}, { | |
"environment": { | |
"development": { | |
"dev": null, | |
"test": null | |
}, | |
"production": { | |
"stage": null, | |
"prod": null | |
} | |
} | |
}, { | |
"lang": { | |
"ar": { | |
"ar-JO": null, | |
"ar-MA": null, | |
"ar-SA": null, | |
"ar-EG": null | |
}, | |
"bn": { | |
"bn-IN": null | |
}, | |
"ca": { | |
"ca-ES": null | |
}, | |
"cs": { | |
"cs-CZ": null | |
}, | |
"da": { | |
"da-DK": null | |
}, | |
"de": { | |
"de-AT": null, | |
"de-DE": null | |
}, | |
"el": { | |
"el-GR": null | |
}, | |
"en": { | |
"en-AU": null, | |
"en-BG": null, | |
"en-CA": null, | |
"en-GB": null, | |
"en-GY": null, | |
"en-HK": null, | |
"en-IE": null, | |
"en-IN": null, | |
"en-MY": null, | |
"en-NZ": null, | |
"en-PH": null, | |
"en-SG": null, | |
"en-US": null, | |
"en-ZA": null | |
}, | |
"es": { | |
"es-AR": null, | |
"es-BO": null, | |
"es-CL": null, | |
"es-CO": null, | |
"es-EC": null, | |
"es-ES": null, | |
"es-MX": null, | |
"es-PE": null, | |
"es-PY": null, | |
"es-US": null, | |
"es-UY": null, | |
"es-VE": null | |
}, | |
"fi": { | |
"fi-FI": null | |
}, | |
"fr": { | |
"fr-BE": null, | |
"fr-CA": null, | |
"fr-FR": null, | |
"fr-GF": null | |
}, | |
"hi": { | |
"hi-IN": null | |
}, | |
"hu": { | |
"hu-HU": null | |
}, | |
"id": { | |
"id-ID": null | |
}, | |
"it": { | |
"it-IT": null | |
}, | |
"ja": { | |
"ja-JP": null | |
}, | |
"kn": { | |
"kn-IN": null | |
}, | |
"ko": { | |
"ko-KR": null | |
}, | |
"ml": { | |
"ml-IN": null | |
}, | |
"mr": { | |
"mr-IN": null | |
}, | |
"ms": { | |
"ms-MY": null | |
}, | |
"nb": { | |
"nb-NO": null | |
}, | |
"nl": { | |
"nl-BE": null, | |
"nl-NL": null, | |
"nl-SR": null | |
}, | |
"pl": { | |
"pl-PL": null | |
}, | |
"pt": { | |
"pt-BR": null | |
}, | |
"ro": { | |
"ro-RO": null | |
}, | |
"ru": { | |
"ru-RU": null | |
}, | |
"sv": { | |
"sv-SE": null | |
}, | |
"ta": { | |
"ta-IN": null | |
}, | |
"te": { | |
"te-IN": null | |
}, | |
"th": { | |
"th-TH": null | |
}, | |
"tr": { | |
"tr-TR": null | |
}, | |
"vi": { | |
"vi-VN": null | |
}, | |
"zh": { | |
"zh-Hans": { | |
"zh-Hans-CN": null | |
}, | |
"zh-Hant": { | |
"zh-Hant-HK": null, | |
"zh-Hant-TW": null | |
} | |
} | |
} | |
}] | |
}] | |
}; | |
suite.add('Recursive Deep Copy', function () { | |
var dimensionsCopy = recursiveDeepCopy(dimensions); | |
}) | |
.add('JSON Deep Copy', function () { | |
var dimensionsCopy = jsonDeepCopy(dimensions); | |
}) | |
.on('cycle', function (event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function () { | |
console.log('Fastest is ' + this.filter('fastest').pluck('name')); | |
}) | |
.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment