Skip to content

Instantly share code, notes, and snippets.

@iki iki/.gitignore forked from jeremychone/yaml-vs-json-perf.js
Last active Nov 12, 2019

Embed
What would you like to do?
# Format: https://git-scm.com/docs/gitignore#_pattern_format
*.json
*.yaml
.DS_Store
*.log
*.log.*
/.*/
/node_modules/

Compare JavaScript/Node JSON and YAML parsers

$ yaml-vs-json-perf.cmd write
json stringify 207ms
js-yaml stringify 13913ms
js-yaml safe stringify 14076ms
yamljs stringify 1213ms
yaml stringify 197367ms

$ yaml-vs-json-perf.cmd read
json parsing 187ms
js-yaml parsing 633ms
js-yaml safe parsing 716ms
yamljs parsing 4644ms
yaml parsing 5792ms

@node "%~dpn0.js" %*
#!/usr/bin/env node
const { router } = require('cmdrouter');
const fs = require('fs-extra');
const yamljs = require('yamljs');
const jsyaml = require('js-yaml');
const yaml = require('yaml');
// USAGE:
// `node yaml-vs-json-perf write` to create the data data yaml and data.json files, and stringify perf measure.
// `node yaml-vs-json-perf read` to perf measure the parsing.
router({ read, write }).route();
async function write() {
const itemsCount = 100000;
const subItemsCount = 3;
let start, encodedString;
const docObj = {};
docObj.items = [];
// create the docObj
for (let i = 0; i < itemsCount; i++) {
const item = { i };
item.name = `name ${i}`;
item.description = `description for ${i}`;
item.subItems = [];
for (let j = 0; j < subItemsCount; j++) {
item.subItems.push(`subItem ${i}-${j}`);
}
docObj.items.push(item);
}
// json
encodedString = '';
start = now();
encodedString = JSON.stringify(docObj, null, ' ');
console.log(`json stringify ${Math.round(now() - start)}ms`);
await fs.writeFile('data.json', encodedString, 'utf8');
// Generate js-yaml
encodedString = '';
start = now();
encodedString = jsyaml.dump(docObj, { indent: 2 });
console.log(`js-yaml stringify ${Math.round(now() - start)}ms`);
await fs.writeFile('data-jsyaml.yaml', encodedString, 'utf8');
// Generate js-yaml safe
encodedString = '';
start = now();
encodedString = jsyaml.safeDump(docObj, { indent: 2 });
console.log(`js-yaml safe stringify ${Math.round(now() - start)}ms`);
// Generate yamljs
encodedString = '';
start = now();
encodedString = yamljs.stringify(docObj, 100, 2);
console.log(`yamljs stringify ${Math.round(now() - start)}ms`);
await fs.writeFile('data-yamljs.yaml', encodedString, 'utf8');
// Generate yaml
encodedString = '';
start = now();
encodedString = yaml.stringify(docObj);
console.log(`yaml stringify ${Math.round(now() - start)}ms`);
await fs.writeFile('data-yaml.yaml', encodedString, 'utf8');
}
async function read() {
let start;
const jsonString = await fs.readFile('data.json', 'utf8');
const jsyamlString = await fs.readFile('data-jsyaml.yaml', 'utf8');
const yamljsString = await fs.readFile('data-yamljs.yaml', 'utf8');
const yamlString = await fs.readFile('data-yaml.yaml', 'utf8');
// pick one to make sure that all are measured with the same input
const yamlTestString = yamljsString;
// json
start = now();
JSON.parse(jsonString);
console.log(`json parsing ${Math.round(now() - start)}ms`);
// js-yaml
start = now();
jsyaml.load(yamlTestString);
console.log(`js-yaml parsing ${Math.round(now() - start)}ms`);
// js-yaml safe
start = now();
jsyaml.safeLoad(yamlTestString);
console.log(`js-yaml safe parsing ${Math.round(now() - start)}ms`);
// yamljs
start = now();
yamljs.parse(yamlTestString);
console.log(`yamljs parsing ${Math.round(now() - start)}ms`);
// yaml
start = now();
yaml.parse(yamlTestString);
console.log(`yaml parsing ${Math.round(now() - start)}ms`);
}
function now() {
var hrTime = process.hrtime();
return hrTime[0] * 1000 + hrTime[1] / 1000000;
}
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
"@babel/runtime@^7.6.3":
version "7.7.2"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.7.2.tgz#111a78002a5c25fc8e3361bedc9529c696b85a6a"
integrity sha512-JONRbXbTXc9WQE2mAZd1p0Z3DZ/6vaQIkgYMSTP3KjRCyd7rCZCcfhCyX+YjwcKxcZ82UrxbRD358bpExNgrjw==
dependencies:
regenerator-runtime "^0.13.2"
argparse@^1.0.7:
version "1.0.10"
resolved "https://registry.yarnpkg.com/argparse/-/argparse-1.0.10.tgz#bcd6791ea5ae09725e17e5ad988134cd40b3d911"
integrity sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==
dependencies:
sprintf-js "~1.0.2"
balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c=
brace-expansion@^1.1.7:
version "1.1.11"
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
integrity sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==
dependencies:
balanced-match "^1.0.0"
concat-map "0.0.1"
cmdrouter@^0.3.1:
version "0.3.1"
resolved "https://registry.yarnpkg.com/cmdrouter/-/cmdrouter-0.3.1.tgz#9cf461db95a4c59c01deb56b2181f1573914a842"
integrity sha512-ZarDACzn72HE3+5P4OH/TM36EpnRksvH6vZtA+Ur6fY7cDOsIw5PYvtU0GtshTq9pYtAK2M6R5MM6rWHR/Vqtw==
concat-map@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
integrity sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=
esprima@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/esprima/-/esprima-4.0.1.tgz#13b04cdb3e6c5d19df91ab6987a8695619b0aa71"
integrity sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==
fs-extra@^8.1.0:
version "8.1.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-8.1.0.tgz#49d43c45a88cd9677668cb7be1b46efdb8d2e1c0"
integrity sha512-yhlQgA6mnOJUKOsRUFsgJdQCvkKhcz8tlZG5HBQfReYZy46OwLcY+Zia0mtdHsOo9y/hP+CxMN0TU9QxoOtG4g==
dependencies:
graceful-fs "^4.2.0"
jsonfile "^4.0.0"
universalify "^0.1.0"
fs.realpath@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f"
integrity sha1-FQStJSMVjKpA20onh8sBQRmU6k8=
glob@^7.0.5:
version "7.1.6"
resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.6.tgz#141f33b81a7c2492e125594307480c46679278a6"
integrity sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==
dependencies:
fs.realpath "^1.0.0"
inflight "^1.0.4"
inherits "2"
minimatch "^3.0.4"
once "^1.3.0"
path-is-absolute "^1.0.0"
graceful-fs@^4.1.6, graceful-fs@^4.2.0:
version "4.2.3"
resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.3.tgz#4a12ff1b60376ef09862c2093edd908328be8423"
integrity sha512-a30VEBm4PEdx1dRB7MFK7BejejvCvBronbLjht+sHuGYj8PHs7M/5Z+rt5lw551vZ7yfTCj4Vuyy3mSJytDWRQ==
inflight@^1.0.4:
version "1.0.6"
resolved "https://registry.yarnpkg.com/inflight/-/inflight-1.0.6.tgz#49bd6331d7d02d0c09bc910a1075ba8165b56df9"
integrity sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=
dependencies:
once "^1.3.0"
wrappy "1"
inherits@2:
version "2.0.4"
resolved "https://registry.yarnpkg.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
js-yaml@^3.13.1:
version "3.13.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.13.1.tgz#aff151b30bfdfa8e49e05da22e7415e9dfa37847"
integrity sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==
dependencies:
argparse "^1.0.7"
esprima "^4.0.0"
jsonfile@^4.0.0:
version "4.0.0"
resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-4.0.0.tgz#8771aae0799b64076b76640fca058f9c10e33ecb"
integrity sha1-h3Gq4HmbZAdrdmQPygWPnBDjPss=
optionalDependencies:
graceful-fs "^4.1.6"
minimatch@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.4.tgz#5166e286457f03306064be5497e8dbb0c3d32083"
integrity sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==
dependencies:
brace-expansion "^1.1.7"
once@^1.3.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
integrity sha1-WDsap3WWHUsROsF9nFC6753Xa9E=
dependencies:
wrappy "1"
path-is-absolute@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/path-is-absolute/-/path-is-absolute-1.0.1.tgz#174b9268735534ffbc7ace6bf53a5a9e1b5c5f5f"
integrity sha1-F0uSaHNVNP+8es5r9TpanhtcX18=
regenerator-runtime@^0.13.2:
version "0.13.3"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.3.tgz#7cf6a77d8f5c6f60eb73c5fc1955b2ceb01e6bf5"
integrity sha512-naKIZz2GQ8JWh///G7L3X6LaQUAMp2lvb1rvwwsURe/VXwD6VMfr+/1NuNw3ag8v2kY1aQ/go5SNn79O9JU7yw==
sprintf-js@~1.0.2:
version "1.0.3"
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
integrity sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=
universalify@^0.1.0:
version "0.1.2"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-0.1.2.tgz#b646f69be3942dabcecc9d6639c80dc105efaa66"
integrity sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==
wrappy@1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/wrappy/-/wrappy-1.0.2.tgz#b5243d8f3ec1aa35f1364605bc0d1036e30ab69f"
integrity sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=
yaml@^1.7.2:
version "1.7.2"
resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.7.2.tgz#f26aabf738590ab61efaca502358e48dc9f348b2"
integrity sha512-qXROVp90sb83XtAoqE8bP9RwAkTTZbugRUTm5YeFCBfNRPEp2YzTeqWiz7m5OORHzEvrA/qcGS8hp/E+MMROYw==
dependencies:
"@babel/runtime" "^7.6.3"
yamljs@^0.3.0:
version "0.3.0"
resolved "https://registry.yarnpkg.com/yamljs/-/yamljs-0.3.0.tgz#dc060bf267447b39f7304e9b2bfbe8b5a7ddb03b"
integrity sha512-C/FsVVhht4iPQYXOInoxUM/1ELSf9EsgKH34FofQOp6hwCPrW4vG4w5++TED3xRUo8gD7l0P1J1dLlDYzODsTQ==
dependencies:
argparse "^1.0.7"
glob "^7.0.5"
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.