Skip to content

Instantly share code, notes, and snippets.

@pranaygp
Created March 22, 2018 06:34
Show Gist options
  • Save pranaygp/ef51a3d207b5e1a8166314d8e292df64 to your computer and use it in GitHub Desktop.
Save pranaygp/ef51a3d207b5e1a8166314d8e292df64 to your computer and use it in GitHub Desktop.
JSON reviver time test created by pranaygp - https://repl.it/@pranaygp/JSON-reviver-time-test
const string = JSON.stringify({
a: 1,
b: 1,
c: 1,
d: 1,
e: 1,
f: 1,
g: 1,
h: 1,
a1: 1,
b1: 1,
c1: 1,
d1: 1,
e1: 1,
f1: 1,
g1: 1,
h1: 1,
a2: 1,
b2: 1,
c2: 1,
d2: 1,
e2: 1,
f2: 1,
g2: 1,
h2: 1,
a3: 1,
b3: 1,
c3: 1,
d3: 1,
e3: 1,
f3: 1,
g3: 1,
h3: 1,
a4: 1,
b4: 1,
c4: 1,
d4: 1,
e4: 1,
f4: 1,
g4: 1,
h4: 1,
a5: 1,
b5: 1,
c5: 1,
d5: 1,
e5: 1,
f5: 1,
g5: 1,
h5: 1,
a6: 1,
b6: 1,
c6: 1,
d6: 1,
e6: 1,
f6: 1,
g6: 1,
h6: 1,
a7: 1,
b7: 1,
c7: 1,
d7: 1,
e7: 1,
f7: 1,
g7: 1,
h7: 1,
})
// setup timing variables
let elapsed = 0
let sTime;
// full parse
for(let i = 0; i < 1e5; i++){
sTime = process.hrtime();
const obj = JSON.parse(string)
obj.a;
let diffTime = process.hrtime(sTime)
elapsed += diffTime[1];
}
console.log(elapsed);
// reset elapsed
elapsed = 0
// partial parse
const reader = (key, val) => {
if (key === 'a') {
val;
let diffTime = process.hrtime(sTime)
elapsed += diffTime[1];
}
}
for(let i = 0; i < 1e5; i++){
sTime = process.hrtime();
JSON.parse(string, reader);
}
console.log(elapsed);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment