Skip to content

Instantly share code, notes, and snippets.

@terrybleger
Last active November 5, 2016 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terrybleger/f9452af98ff1582b784f9bf473b8f31a to your computer and use it in GitHub Desktop.
Save terrybleger/f9452af98ff1582b784f9bf473b8f31a to your computer and use it in GitHub Desktop.
Large JSON / Object test (in stupid way)
"use strict";
Promise = require("bluebird");
var fs = require("fs");
var moment = require("moment");
var sample = [];
var start, duration;
// const ROWS = 2 * 1000 * 1000; // RangeError: Invalid string length
const ROWS = 1 * 1000 * 1000;
function beginCount() {
start = moment();
}
function endCount(desc) {
duration = moment.duration(moment().diff(start));
console.log(desc, ":", duration.asMilliseconds());
}
function benchmark_a() {
return new Promise((resolve, reject) => {
try {
beginCount();
for (let i = 0; i <= ROWS; i++) {
sample.push({
"ACCOUNT_NUMBER":"1234567890",
"CUSTOMER_NAME":"ACME Products and Services, Inc.",
"ADDRESS":"123 Main Street",
"CITY":"Albuquerque",
"STATE":"NM",
"ZIP":"87101-1234"
});
}
endCount("filling");
beginCount();
let data = JSON.stringify(sample);
endCount("stringify");
beginCount();
fs.writeFile(__dirname + "/sample.json", data, (err) => {
if (!err) {
endCount("writing");
resolve();
} else {
reject(err);
}
});
} catch (err) {
reject(err);
}
});
}
function benchmark_b() {
return new Promise((resolve, reject) => {
try {
beginCount();
fs.readFile(__dirname + "/sample.json", (err, data) => {
if (!err) {
endCount("reading");
beginCount();
sample = JSON.parse(data);
endCount("parse");
resolve();
} else {
reject(err);
}
});
} catch (err) {
reject(err);
}
});
}
Promise.all([])
.then(benchmark_a)
.then(benchmark_b)
.then(() => { console.log("done"); })
.catch((err) => { console.error(err); });
mkdir test && cd test
touch index.js
npm init
npm install --save bluebird moment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment