Large JSON / Object test (in stupid way)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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