Skip to content

Instantly share code, notes, and snippets.

@jasondavies
Last active Aug 29, 2015
Embed
What would you like to do?
d3.csv benchmark
#!/usr/bin/env node
var d3 = require("./");
var csv = ["foo", "bar", "baz", "foobarbaz", "BLAHBLAH"].join(",") + "\n" +
d3.csv.formatRows(d3.range(10000).map(function() {
return d3.range(5).map(Math.random);
}));
benchmark("Slow", function() { parseSlow(csv); });
benchmark("Fast", function() { parseFast(csv); });
benchmark("JSON", function() { parseJSON(csv); });
function parseSlow(text, f) {
var header, m = 0;
return d3.csv.parseRows(text, function(row, i) {
if (i) {
var o = {}, j = -1;
while (++j < m) o[header[j]] = row[j];
return o;
}
header = row;
m = header.length;
return null;
});
}
function parseFast(text, f) {
var o;
return d3.csv.parseRows(text, function(row, i) {
if (o) return o(row, i - 1);
var a = new Function("d", "return {" + row.map(function(name, i) {
return JSON.stringify(name) + ": d[" + i + "]";
}).join(",") + "}");
o = f ? function(row, i) { return f(a(row), i); } : a;
});
}
function parseJSON(text, f) {
var o;
return d3.csv.parseRows(text, function(row, i) {
if (o) return o(row, i - 1);
var a = function(d) {
return JSON.parse("{" + row.map(function(name, i) {
return JSON.stringify(name) + ":" + JSON.stringify(d[i]);
}).join(",") + "}");
};
o = f ? function(row, i) { return f(a(row), i); } : a;
});
}
function benchmark(name, f) {
var observations = d3.range(20).map(function() {
process.stdout.write(".");
var start = process.hrtime();
f();
var elapsed = process.hrtime(start);
return elapsed[0] * 1e3 + elapsed[1] / 1e6;
});
console.log("");
console.log(name + ": " + Math.round(d3.mean(observations)) + "ms.");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment