Skip to content

Instantly share code, notes, and snippets.

@janroesner
Created August 30, 2019 18:59
Show Gist options
  • Save janroesner/317164d330ca1b6bc3b8f77d6e4dc06a to your computer and use it in GitHub Desktop.
Save janroesner/317164d330ca1b6bc3b8f77d6e4dc06a to your computer and use it in GitHub Desktop.
// Number of objects in the JSON (reduce it to 20 ... no much difference!!!)
const NUMBER_OF_ENTRIES = 5800;
const numbers = [...Array(NUMBER_OF_ENTRIES).keys()];
// Generating an arbitrary Array of objects w/o additional IDs (the "normal" version)
const slowJSON = numbers.map(el => {
return { id: el, value: String(Math.ceil(Math.random() * 1000000)) };
});
// Generating an arbitrary Object using additional ID's (a version optimized for the search cases we will have in the frontend)
const fastJSON = numbers.reduce((obj, el) => {
return {
...obj,
[el]: { id: el, value: String(Math.ceil(Math.random() * 1000000)) }
};
}, {});
// Randomly generate an ID for an object to be selected
const selectable = Math.ceil(Math.random() * 5800);
// Timing the slow version
console.time("Array");
const result = slowJSON.filter(el => {
return el.id === selectable;
});
console.timeEnd("Array");
// Timing the fast version
console.time("Object");
const result2 = fastJSON[selectable];
console.timeEnd("Object");
// Simple transform to turn the "fast" object structure into the "slow" one (what would have to be done on the JAVA side)
const transform = Object.keys(fastJSON).map(id => fastJSON[id]);
// console.log("Transform: ", transform);
@janroesner
Copy link
Author

Bildschirmfoto 2019-08-30 um 21 00 15

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