Created
August 30, 2019 18:59
-
-
Save janroesner/317164d330ca1b6bc3b8f77d6e4dc06a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
Author
janroesner
commented
Aug 30, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment