Skip to content

Instantly share code, notes, and snippets.

@baptistemanson
Created February 10, 2021 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baptistemanson/ce7f6ddf777be2e849eb4c4ad7ae6a79 to your computer and use it in GitHub Desktop.
Save baptistemanson/ce7f6ddf777be2e849eb4c4ad7ae6a79 to your computer and use it in GitHub Desktop.
demo hashmap to structs
const fs = require("fs");
function dateCompress(d) {
return d.replace(/\.\d{3}\+\d{2}:\d{2}$/g, "Z");
}
function getSize(d) {
return Math.ceil(d.length / 1024).toLocaleString() + "kB";
}
function compareSize(a, b) {
console.log("uncompressed", getSize(JSON.stringify(a)));
console.log(" compressed", getSize(JSON.stringify(b)));
}
let rawdata = fs.readFileSync("./personal-2021-2-10.if").toString();
let data = JSON.parse(rawdata);
let uncompressedEntities = data.entities;
let compressedEntities = [];
// {"id":"1","content":"phoebe","createdAt":"2021-02-08T20:48:46.569+00:00","updatedAt":"2021-02-08T20:48:46.569+00:00"}
Object.values(uncompressedEntities).forEach((e) => {
let entity = [
e.id,
e.content,
dateCompress(e.createdAt),
dateCompress(e.updatedAt),
];
compressedEntities.push(entity);
});
compareSize(uncompressedEntities, compressedEntities);
// {
// "id":"BcIfWvz3-8",
// "tokens":[
// {"type":"paragraph",
// "content":[
// {"type":"text",
// "content":"i am excited to become more proficient in who i am"},
// {"type":"author","entityId":"148"},
// {"type":"text","content":" varga"},
// {"type":"hashtag","content":"#quote"},
// {"type":"text","content":" on growing up and going to college"}
// ]}],
// "createdAt":"2021-02-08T20:48:46.760Z",
// "updatedAt":"2021-02-08T20:48:48.220Z",
// "position":"bqi",
// "insertedAt":"00000000",
// ]
// "authorId":"google-oauth2|112556675592475315585"}
let notes = data.notes;
function compressToken(t) {
switch (t) {
case "paragraph":
return ["p", compressToken(t.content)];
case "text":
return ["t", t.content];
case "author":
return ["a", t.entityId];
case "mention":
return ["m", t.entityId];
case "hashtag":
return ["h", t.content];
default:
return [t.type, t]; // fallback for unknown tokens
}
}
function compressTokens(tks) {
tks.map(compressToken);
}
let time = Date.now();
let compressedNotes = [];
Object.values(notes).forEach((n) => {
let note = [
n.id,
compressTokens(n.tokens),
dateCompress(n.createdAt),
dateCompress(n.updatedAt),
position,
n.insertedAt,
n.authorId,
];
compressedNotes.push(note);
});
console.log(Date.now() - time);
compareSize(notes, compressedNotes);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment