Create a gist now

Instantly share code, notes, and snippets.

anonymous /index.html
Created Jan 21, 2013

What would you like to do?
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}
</style>
<body>
<script src="http://d3js.org/d3.v3.js"></script>
<script>
var margin = {top: 20, right: 80, bottom: 30, left: 50},
width = 640 - margin.left - margin.right,
height = 200 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.ticks(5)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.tickFormat(function(d) {
return Math.round(d / (1024 * 1024)) + 'mb';
});
var line = d3.svg.line()
.x(function(d) { return x(d[0]); })
.y(function(d) { return y(d[1]); });
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
d3.json("new.json", function(error, ndata) {
d3.json("module.json", function(error, mdata) {
color.domain(['n', 'm']);
x.domain(d3.extent(ndata, function(d) { return d[0]; }));
y.domain([0, d3.max([
d3.max(ndata, function(d) { return d[1]; }),
d3.max(mdata, function(d) { return d[1]; })
])]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Memory Usage");
var city = svg.selectAll(".city")
.data([mdata, ndata])
.enter().append("g")
.attr("class", "city");
city.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d); })
.style("stroke", function(d) { return color(d); });
});
});
</script>
module.exports = function instancecounter(c) {
var cars = [];
var reads = [];
for (var i = 0; i < 1000000; i++) {
cars.push(c());
if (!(i & (i - 1))) {
reads.push([i, process.memoryUsage().heapUsed]);
}
}
return reads;
};
function car() {
var c = {},
direction = 0;
c.turn = function(angle) {
direction += angle;
return c;
};
return c;
}
var instancecounter = require('./instancecounter');
console.log(JSON.stringify(instancecounter(car)));
[[0,2243136],[1,2245224],[2,2246112],[4,2246648],[8,2247584],[16,2249320],[32,2253016],[64,2260224],[128,2274296],[256,2304744],[512,2366040],[1024,2485704],[2048,2391480],[4096,2836696],[8192,3730376],[16384,5597432],[32768,8628784],[65536,15793608],[131072,30275448],[262144,60761920],[524288,117828816]]
function car() {
this.direction = 0;
}
car.prototype.turn = function(angle) {
this.direction += angle;
return this;
};
function makecar() {
return new car();
}
var instancecounter = require('./instancecounter');
console.log(JSON.stringify(instancecounter(makecar)));
[[0,2244016],[1,2245552],[2,2246024],[4,2246352],[8,2247088],[16,2247480],[32,2248488],[64,2250320],[128,2253640],[256,2262584],[512,2278648],[1024,2312296],[2048,2384392],[4096,2107312],[8192,2372032],[16384,2808600],[32768,3303056],[65536,4691000],[131072,8467672],[262144,15341880],[524288,28368176]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment