Skip to content

Commit

Permalink
Snapshot compare refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
3y3 committed May 1, 2015
1 parent e289ef6 commit 7823c85
Showing 1 changed file with 27 additions and 32 deletions.
59 changes: 27 additions & 32 deletions v8-profiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,42 @@ Snapshot.prototype.getHeader = function() {
}
}

Snapshot.prototype.compare = function (other) {
var my_objects = this.nodeCounts(),
their_objects = other.nodeCounts(),
diff = {}, i, k, my_val, their_val,
all_keys = Object.keys(my_objects).concat(Object.keys(their_objects)); //has dupes, oh well
for (i = 0; i < all_keys.length; i++) {
k = all_keys[i];
my_val = my_objects[k] || 0;
their_val = their_objects[k] || 0;
diff[k] = their_val - my_val;
}
/**
* @param {Snapshot} other
* @returns {Object}
*/
Snapshot.prototype.compare = function(other) {
var selfHist = nodesHist(this),
otherHist = nodesHist(other),
keys = Object.keys(selfHist).concat(Object.keys(otherHist)),
diff = {};

keys.forEach(function(key) {
if (key in diff) return;

var selfCount = selfCounts[key] || 0,
otherCount = otherCounts[key] || 0;

diff[key] = otherCount - selfCount;
});

return diff;
};

Snapshot.prototype.allNodes = function() {
var n = this.nodesCount, i, nodes = [];
function nodes(snapshot) {
var n = snapshot.nodesCount, i, nodes = [];
for (i = 0; i < n; i++) {
nodes[i] = this.getNode(i);
nodes[i] = snapshot.getNode(i);
}
return nodes;
};

Snapshot.prototype.nodeCounts = function() {
function nodesHist(snapshot) {
var objects = {};
this.allNodes().forEach(function(n){
if(n.type === "Object") {
if (objects[n.name]) {
objects[n.name] += 1;
}
else {
objects[n.name] = 1;
}
}
else {
if (objects[n.type]) {
objects[n.type] += 1;
}
else {
objects[n.type] = 1;
}
}
nodes(snapshot).forEach(function(node){
var key = node.type === "Object" ? node.name : node.type;
objects[key] = objects[node.name] || 0;
objects[key]++;
});
return objects;
};
Expand Down

0 comments on commit 7823c85

Please # to comment.