Created
January 22, 2019 03:01
-
-
Save colus001/a889c563da0b90862a16e743df6708fe to your computer and use it in GitHub Desktop.
Measure Node.js File IO in terms of copy and write
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
const fs = require('fs'); | |
const path = require('path'); | |
const rimraf = require('rimraf'); | |
const ProgressBar = require('progress'); | |
const readFrom = path.join(__dirname, './test.json'); | |
const file = fs.readFileSync(readFrom); | |
const getRandomNumber = (number = 1000000) => Math.floor(Math.random() * number); | |
const LENGTH = 10000; | |
const measure = (label, callback) => { | |
console.time(label); | |
var bar = new ProgressBar(' measuring ":label" [:bar] :percent :etas', { | |
complete: '=', | |
incomplete: ' ', | |
width: 40, | |
total: LENGTH | |
}); | |
Array.from({ length: LENGTH }).map(() => { | |
callback(); | |
bar.tick({ label }); | |
}) | |
console.timeEnd(label); | |
} | |
measure('write', () => { | |
const writeTo = path.join(__dirname, `./test.json.write.${getRandomNumber()}`); | |
fs.writeFileSync(writeTo, file, 'utf8'); | |
}); | |
measure('copy', () => { | |
const copyTo = path.join(__dirname, `./test.json.copy.${getRandomNumber()}`); | |
fs.copyFileSync(readFrom, copyTo); | |
}); | |
rimraf(path.join(__dirname, './test.json.write.*'), () => {}) | |
rimraf(path.join(__dirname, './test.json.copy.*'), () => {}) |
test.json generated from json-generator
[
{
"_id": "5c46878f39a2bb653ca06ce4",
"index": 0,
"guid": "4f6e9e86-ef95-4f92-97b6-0949bc4122d8",
"isActive": false,
"balance": "$2,166.92",
"picture": "http://placehold.it/32x32",
"age": 27,
"eyeColor": "blue",
"name": "Evans Callahan",
"gender": "male",
"company": "OBONES",
"email": "[email protected]",
"phone": "+1 (946) 584-3326",
"address": "222 Sullivan Place, Catherine, North Dakota, 8964",
"about": "Aliqua consectetur nulla incididunt dolor sint ipsum do cupidatat occaecat. Exercitation mollit adipisicing ut in commodo id. Non culpa et anim veniam nulla.\r\n",
"registered": "2017-03-26T02:23:05 -09:00",
"latitude": -24.554138,
"longitude": 56.904087,
"tags": [
"non",
"qui",
"et",
"minim",
"dolor",
"laborum",
"officia"
],
"friends": [
{
"id": 0,
"name": "Solomon Witt"
},
{
"id": 1,
"name": "Larson Dorsey"
},
{
"id": 2,
"name": "Lynette Mcguire"
}
],
"greeting": "Hello, Evans Callahan! You have 1 unread messages.",
"favoriteFruit": "apple"
},
{
"_id": "5c46878f5c46628c7ac61b47",
"index": 1,
"guid": "9ab161a4-8ba2-4043-962f-fb6c5052276c",
"isActive": false,
"balance": "$3,333.50",
"picture": "http://placehold.it/32x32",
"age": 24,
"eyeColor": "green",
"name": "Cotton Jennings",
"gender": "male",
"company": "EARGO",
"email": "[email protected]",
"phone": "+1 (810) 569-3865",
"address": "684 Emerson Place, Sultana, Michigan, 2565",
"about": "Voluptate deserunt nostrud culpa sint culpa quis eiusmod incididunt exercitation. Duis do proident ea nostrud duis velit. Esse voluptate cupidatat labore sunt ut dolor in aute.\r\n",
"registered": "2014-10-31T04:36:59 -09:00",
"latitude": -87.952013,
"longitude": -168.673735,
"tags": [
"nostrud",
"eu",
"reprehenderit",
"magna",
"aliquip",
"deserunt",
"cillum"
],
"friends": [
{
"id": 0,
"name": "Letha Miles"
},
{
"id": 1,
"name": "Yang Velazquez"
},
{
"id": 2,
"name": "Cecilia Cannon"
}
],
"greeting": "Hello, Cotton Jennings! You have 2 unread messages.",
"favoriteFruit": "apple"
},
{
"_id": "5c46878f2d4233656c956f68",
"index": 2,
"guid": "6cb87997-b267-4baa-a5e6-49fceddca690",
"isActive": false,
"balance": "$1,776.68",
"picture": "http://placehold.it/32x32",
"age": 29,
"eyeColor": "brown",
"name": "Elizabeth Browning",
"gender": "female",
"company": "TERRAGEN",
"email": "[email protected]",
"phone": "+1 (985) 425-3684",
"address": "778 Croton Loop, Sims, Nebraska, 5562",
"about": "Elit enim mollit cupidatat pariatur reprehenderit do eiusmod id nulla. Fugiat sit nostrud voluptate mollit ut ea voluptate excepteur aliquip non. Ad nostrud sit adipisicing commodo proident pariatur voluptate velit commodo magna. Eiusmod voluptate id ullamco cupidatat exercitation magna culpa cupidatat. Do fugiat id deserunt ut fugiat consectetur ipsum ea excepteur. Dolor et ipsum proident commodo reprehenderit nulla quis laborum in.\r\n",
"registered": "2018-04-09T05:31:36 -09:00",
"latitude": -36.65863,
"longitude": 111.04643,
"tags": [
"mollit",
"commodo",
"qui",
"incididunt",
"reprehenderit",
"cupidatat",
"ex"
],
"friends": [
{
"id": 0,
"name": "Meyers Douglas"
},
{
"id": 1,
"name": "Sutton Simon"
},
{
"id": 2,
"name": "Landry Larsen"
}
],
"greeting": "Hello, Elizabeth Browning! You have 3 unread messages.",
"favoriteFruit": "strawberry"
},
{
"_id": "5c46878f0d12c505a65b09dd",
"index": 3,
"guid": "11888d89-5665-41f8-b096-e6ebaa7f922f",
"isActive": false,
"balance": "$3,474.75",
"picture": "http://placehold.it/32x32",
"age": 25,
"eyeColor": "blue",
"name": "Lula Sherman",
"gender": "female",
"company": "MAZUDA",
"email": "[email protected]",
"phone": "+1 (851) 558-3806",
"address": "858 Williams Avenue, Oretta, Georgia, 4647",
"about": "Non et deserunt pariatur pariatur veniam consectetur Lorem laboris aute elit. Sunt exercitation ea eu non ea est elit occaecat ad. Nisi nulla labore laboris ea ex eu magna est exercitation. Quis sit proident est elit laboris. Pariatur adipisicing nostrud deserunt fugiat eu veniam Lorem consequat aliquip velit proident labore sunt. Anim adipisicing velit reprehenderit deserunt non aute irure excepteur incididunt.\r\n",
"registered": "2015-08-16T09:44:30 -09:00",
"latitude": -86.485603,
"longitude": 13.579654,
"tags": [
"laboris",
"non",
"incididunt",
"excepteur",
"dolore",
"dolor",
"est"
],
"friends": [
{
"id": 0,
"name": "Schmidt Keith"
},
{
"id": 1,
"name": "Browning Cleveland"
},
{
"id": 2,
"name": "Wiley Beasley"
}
],
"greeting": "Hello, Lula Sherman! You have 5 unread messages.",
"favoriteFruit": "strawberry"
},
{
"_id": "5c46878fcba2e713143ee2a8",
"index": 4,
"guid": "6e6ae511-2063-4655-a81e-567abcd4531e",
"isActive": false,
"balance": "$1,475.50",
"picture": "http://placehold.it/32x32",
"age": 21,
"eyeColor": "brown",
"name": "Sonya Day",
"gender": "female",
"company": "ENERSAVE",
"email": "[email protected]",
"phone": "+1 (941) 482-2137",
"address": "983 Tehama Street, Itmann, Oregon, 8784",
"about": "Tempor non elit irure ea id enim eu veniam quis. Excepteur amet eu minim excepteur veniam tempor exercitation do reprehenderit non anim qui. Ipsum minim cupidatat fugiat id laborum tempor nulla. Incididunt nisi sunt fugiat elit incididunt consequat dolore. Proident elit mollit ea reprehenderit excepteur voluptate cupidatat. Pariatur esse deserunt occaecat sint in excepteur ad deserunt laborum velit cillum est non eiusmod. Culpa qui ullamco adipisicing esse reprehenderit.\r\n",
"registered": "2017-12-23T05:50:13 -09:00",
"latitude": 43.898764,
"longitude": 160.345053,
"tags": [
"labore",
"ullamco",
"eiusmod",
"sunt",
"dolor",
"fugiat",
"anim"
],
"friends": [
{
"id": 0,
"name": "Tammi Burns"
},
{
"id": 1,
"name": "Gould Mayo"
},
{
"id": 2,
"name": "Celeste Cruz"
}
],
"greeting": "Hello, Sonya Day! You have 7 unread messages.",
"favoriteFruit": "apple"
},
{
"_id": "5c46878f34274fd3ec8ea877",
"index": 5,
"guid": "08ad8de4-9230-47b5-ad83-40ecb42d9e5b",
"isActive": true,
"balance": "$2,119.30",
"picture": "http://placehold.it/32x32",
"age": 31,
"eyeColor": "blue",
"name": "Marta Gilmore",
"gender": "female",
"company": "ENOMEN",
"email": "[email protected]",
"phone": "+1 (806) 458-2331",
"address": "372 Court Square, Rodman, Pennsylvania, 3334",
"about": "Officia incididunt quis sit eiusmod amet exercitation fugiat laborum dolore in reprehenderit nostrud minim. Veniam aute non irure adipisicing nostrud officia commodo aliqua non est pariatur veniam quis ea. Commodo duis esse ut dolor aliquip consectetur in pariatur eu laborum. Ipsum aute ea ipsum excepteur enim enim velit deserunt officia do cupidatat. Aliquip consectetur consectetur id culpa ad. Nisi velit Lorem aute non enim occaecat minim reprehenderit fugiat non veniam cillum.\r\n",
"registered": "2016-02-17T01:31:37 -09:00",
"latitude": -68.542172,
"longitude": -41.505057,
"tags": [
"aute",
"ipsum",
"exercitation",
"dolor",
"dolor",
"eu",
"officia"
],
"friends": [
{
"id": 0,
"name": "Wise Kidd"
},
{
"id": 1,
"name": "Weaver Andrews"
},
{
"id": 2,
"name": "Elena Potts"
}
],
"greeting": "Hello, Marta Gilmore! You have 9 unread messages.",
"favoriteFruit": "apple"
},
{
"_id": "5c46878fbd36e61b7d5f0271",
"index": 6,
"guid": "9f7abdc2-feec-4d18-bcdc-7ddd50e5c37a",
"isActive": false,
"balance": "$3,374.69",
"picture": "http://placehold.it/32x32",
"age": 40,
"eyeColor": "green",
"name": "Bolton Hobbs",
"gender": "male",
"company": "OPTIQUE",
"email": "[email protected]",
"phone": "+1 (999) 501-3165",
"address": "948 Grace Court, Neibert, West Virginia, 1072",
"about": "Aute cillum ad eiusmod et ipsum exercitation laborum cillum do ut. Cupidatat velit ex excepteur velit ea. Nostrud minim enim cupidatat enim est laboris id ipsum consequat voluptate ut tempor. Exercitation commodo mollit reprehenderit culpa ex occaecat est eu aliqua veniam quis nostrud eu enim. Velit veniam laborum laborum proident laboris eiusmod sint.\r\n",
"registered": "2016-09-11T10:17:50 -09:00",
"latitude": -2.247817,
"longitude": 136.822919,
"tags": [
"et",
"ex",
"est",
"deserunt",
"ex",
"culpa",
"cupidatat"
],
"friends": [
{
"id": 0,
"name": "Alberta Weber"
},
{
"id": 1,
"name": "Nash Ortiz"
},
{
"id": 2,
"name": "Jane Estrada"
}
],
"greeting": "Hello, Bolton Hobbs! You have 4 unread messages.",
"favoriteFruit": "apple"
}
]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result is below