Skip to content

Instantly share code, notes, and snippets.

@rjmunro rjmunro/walk.js
Created Mar 23, 2018

Embed
What would you like to do?
Script to walk browser-compat-data and find common subtrees
#!/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
You can’t perform that action at this time.