JavaScript Ninja – Heuristic-Algorithms Tip: Page Element Spread/Distribution

Useful for heuristic-algorithms.

NodeList.prototype.forEach = Array.prototype.forEach;

var count = {};
var distrb = {};

var elements = document.querySelectorAll("*");
var length = elements.length;

elements.forEach(function(element){ count[ element.nodeName ] = (count[ element.nodeName ] || 0) +1; });

Object.keys(count).forEach(function(key){
  distrb[ key ] = 100 * count[ key ] / length 
});

This helps to characterization of the page,
for example- this page: https://nodejs.org/download/nightly/v8.0.0-nightly20161123fd644f51f8/

has a very defined percentage of “download page” or “FTP like download page” :)

{
    HTML:   2.1739130434782608
  , HEAD:   2.1739130434782608
  , TITLE:  2.1739130434782608
  , BODY:   2.1739130434782608
  , H1:     2.1739130434782608
  , HR:     4.3478260869565215
  , PRE:    2.1739130434782608
  , A:      80.43478260869566
  , SCRIPT: 2.1739130434782608
}

might come in handy for someone else too… ;]

Leave a Reply