Created
June 22, 2017 15:21
-
-
Save cjsheets/4c67a777e3bfc031b801dbb2eeac3230 to your computer and use it in GitHub Desktop.
Flatten an Object (http://jsbench.github.io/#4c67a777e3bfc031b801dbb2eeac3230) #jsbench #jsperf
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Flatten an Object</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
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
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
suite.add("var flattenMap = function(map,str='',build={}) {", function () { | |
var flattenMap = function(map,str='',build={}) { | |
for (var k in map) { | |
({}.toString.call(map[k]) == '[object Object]') ? flattenMap(map[k],str+k+'/',build) : build[str + k] = map[k]; | |
} | |
return build; | |
}; | |
flattenMap({ | |
'a': { | |
'b': { | |
'c': 12, | |
'd': 'Hello World' | |
}, | |
'e': [1,2,3], | |
'f': 'test', | |
'g': 'test', | |
'h': 'test', | |
'i': 'test', | |
'j': 'test', | |
'k': { | |
'l': 'test', | |
'm': 'test' | |
} | |
} | |
}); | |
}); | |
suite.add("function flattenMap(map) {", function () { | |
function flattenMap(map) { | |
var out = {}; | |
Object.keys(map).forEach(function(p) { | |
if(map[p] && typeof map[p] == 'object' && !Array.isArray(map[p])) { | |
var inner = flattenMap(map[p]); | |
Object.keys(inner).forEach(function(i) { | |
out[p + '/' + i] = inner[i] == null ? null : inner[i]; | |
}); | |
} else { | |
out[p] = map[p] == null ? null : map[p]; | |
} | |
}); | |
return out; | |
} | |
flattenMap({ | |
'a': { | |
'b': { | |
'c': 12, | |
'd': 'Hello World' | |
}, | |
'e': [1,2,3], | |
'f': 'test', | |
'g': 'test', | |
'h': 'test', | |
'i': 'test', | |
'j': 'test', | |
'k': { | |
'l': 'test', | |
'm': 'test' | |
} | |
} | |
}); | |
}); | |
suite.add("function flattenMap(map) {", function () { | |
function flattenMap(map) { | |
var result = {}; | |
function recurse (cur, prop) { | |
if (Object(cur) !== cur || Array.isArray(cur)) { | |
return result[prop] = cur; | |
} | |
for (var p in cur) { | |
recurse(cur[p], prop ? prop+"/"+p : p); | |
} | |
} | |
recurse(map, ""); | |
return result; | |
} | |
flattenMap({ | |
'a': { | |
'b': { | |
'c': 12, | |
'd': 'Hello World' | |
}, | |
'e': [1,2,3], | |
'f': 'test', | |
'g': 'test', | |
'h': 'test', | |
'i': 'test', | |
'j': 'test', | |
'k': { | |
'l': 'test', | |
'm': 'test' | |
} | |
} | |
}); | |
}); | |
suite.add("function flattenMap(map, way, res) {", function () { | |
function flattenMap(map, way, res) { | |
var way = way || [], res = res || {}; | |
for (var key in map) { | |
var arr = way.concat([key]); | |
if ({}.toString.call(map[key]) == '[object Object]') flattenMap(map[key],arr,res) | |
else res[arr.join('/')] = map[key]; | |
} | |
return res; | |
} | |
flattenMap({ | |
'a': { | |
'b': { | |
'c': 12, | |
'd': 'Hello World' | |
}, | |
'e': [1,2,3], | |
'f': 'test', | |
'g': 'test', | |
'h': 'test', | |
'i': 'test', | |
'j': 'test', | |
'k': { | |
'l': 'test', | |
'm': 'test' | |
} | |
} | |
}); | |
}); | |
suite.add("flattenObject(object) {", function () { | |
flattenObject(object) { | |
const flatObject = Object.create(Object.prototype); | |
for (const key in object) { | |
if (!object.hasOwnProperty(key)) { | |
continue; | |
} | |
if (typeof object[key] === 'object') { | |
const subObject = flattenObject(object[key]); | |
for (const subKey in subObject) { | |
if (!subObject.hasOwnProperty(subKey)) { | |
continue; | |
} | |
flatObject[key + '/' + subKey] = subObject[subKey]; | |
} | |
} else { | |
flatObject[key] = object[key]; | |
} | |
} | |
return flatObject; | |
}; | |
flattenObject({ | |
'a': { | |
'b': { | |
'c': 12, | |
'd': 'Hello World' | |
}, | |
'e': [1,2,3], | |
'f': 'test', | |
'g': 'test', | |
'h': 'test', | |
'i': 'test', | |
'j': 'test', | |
'k': { | |
'l': 'test', | |
'm': 'test' | |
} | |
} | |
}); | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("Flatten an Object"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment