Created
March 23, 2018 20:36
-
-
Save rjmunro/1c13b34f2845aeece52b9a2fd0d76ce6 to your computer and use it in GitHub Desktop.
Script to walk browser-compat-data and find common subtrees
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
#!/usr/bin/env node | |
// To use: | |
// npm install object-hash | |
// | |
var Hash = require('object-hash'); | |
var Data = require('.'); | |
var byHash = {} | |
var best = {score: 0}; | |
function analyse(path, data, maxDepth = 6) { | |
for (key of Object.keys(data)) { | |
var hash = Hash(data[key]); | |
var newPath = path + '/' + key | |
if (byHash[hash]) { | |
byHash[hash].count += 1; | |
byHash[hash].score = (byHash[hash].count - 1) * byHash[hash].length * byHash[hash].length; | |
byHash[hash].paths.push(newPath) | |
} else { | |
var length = JSON.stringify(data[key]).length; | |
byHash[hash] = { count: 1, length: length, paths: [newPath], score: 0 }; | |
} | |
if (length > 20 && byHash[hash].score > best.score) { | |
best = byHash[hash]; | |
console.log(best, best.paths[0]); | |
} | |
if (maxDepth && data[key]) { | |
analyse(newPath, data[key], maxDepth - 1); | |
} | |
} | |
} | |
analyse('', Data); | |
result = Object.values(byHash); | |
result.sort((a, b)=> b.score - a.score); | |
console.log(JSON.stringify(result.slice(0,10), null, 2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment