Skip to content

Instantly share code, notes, and snippets.

@wangbinyq
Created January 18, 2017 02:10
Show Gist options
  • Save wangbinyq/2b6def9c39c1d246cb4fd2f60ea6c058 to your computer and use it in GitHub Desktop.
Save wangbinyq/2b6def9c39c1d246cb4fd2f60ea6c058 to your computer and use it in GitHub Desktop.
find how many html element have class or id
(() => {
const $ = document.querySelectorAll.bind(document)
const info = {}
const total = [0, 0]
$('*').forEach((el) => {
const nodeName = el.nodeName
const tagInfo = (info[nodeName] = info[nodeName] || [0, 0])
tagInfo[0] += 1
if(el.className || el.id) {
tagInfo[1] += 1
}
})
Object.keys(info).forEach((key) => {
info[key].push(info[key][1] / info[key][0])
total[0] += info[key][0]
total[1] += info[key][1]
})
console.log(info)
console.log('div', info.DIV)
console.log('span', info.SPAN)
console.log('total', total, total[1] / total[0])
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment