Skip to content

Instantly share code, notes, and snippets.

@terrybleger terrybleger/Preparation
Last active Nov 5, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.