Skip to content

Instantly share code, notes, and snippets.

@nikhedonia
Last active March 16, 2020 10:56
Show Gist options
  • Save nikhedonia/977a70c3a9eb00950e5c1c29d4109430 to your computer and use it in GitHub Desktop.
Save nikhedonia/977a70c3a9eb00950e5c1c29d4109430 to your computer and use it in GitHub Desktop.
tests normalized vs denormalized+gzip data
const {writeFileSync} = require('fs');
const { normalize, schema } = require('normalizr');
const faker = require('faker');
const murmur = require("murmurhash-js");
const genId = (data) => murmur(JSON.stringify(data));
// Define a users schema
const user = new schema.Entity('users', undefined, {
idAttribute: genId
});
// Define your comments schema
const comment = new schema.Entity('comments', {
author: user
});
// Define your article
const article = new schema.Entity('articles', {
authors: [user],
comments: [comment]
});
function range(min, max) {
const count = max - min;
return Array
.from({length: (faker.random.number() % count) + min}, (_, i) => i + min)
}
function pickSample(data, min, max) {
const count = (max||data.length) - min;
return Array
.from({length: (faker.random.number() % count) + min}, () => faker.random.number() % data.length)
.map(i => data[i]);
}
function generateUser() {
return {
name: faker.name.findName(), // Rowan Nikolaus
email: faker.internet.email(), // Kassandra.Haley@erich.biz
card: faker.helpers.userCard() // random contact card containing many properties
};
}
function generateComment(users) {
return {
id: faker.random.uuid(),
body: faker.lorem.paragraph(),
author: users[faker.random.number()%users.length],
};
}
function generateArticle(users) {
const comments = range(0, 5)
.map(() => generateComment(users));
const authors = pickSample(users, 2, 5);
return {
id: faker.random.uuid(),
slug: faker.lorem.slug(),
body: faker.lorem.paragraphs(),
comments,
authors
};
}
function generateArticles() {
const users = Array.from({length: 100}, generateUser);
return Array.from({length: 100}, () => generateArticle(users))
}
const data = generateArticles();
const normalized = normalize(data, [article]);
writeFileSync('./in.json', JSON.stringify(data, null, 2));
writeFileSync('./normalized.json', JSON.stringify(normalized, null, 2));
// gzip -c normalized.json > normalized.json.gz && gzip -c in.json > in.json.gz
@nikhedonia
Copy link
Author

nikhedonia commented Mar 4, 2020

532K input.json (not normalized)
128K input.json.gz (not normalized)

256K normalized.json (50% reusability)
128K normalized.json.gz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment