Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store and retrieve javascript objects? #1099

Open
txtl opened this issue Oct 16, 2023 · 0 comments
Open

Store and retrieve javascript objects? #1099

txtl opened this issue Oct 16, 2023 · 0 comments

Comments

@txtl
Copy link

txtl commented Oct 16, 2023

I got a big JSON file (35MB), which took around 7 ms to fetch and 1000 ms to parse. I'm thinking of storing objects to minimize the parse time. I tried localforage, and found that it took roughly 900 ms to get it from the storage? I thought storing/retrieving JSON objects should not take long, but it seems that I was wrong. I explicitly set the driver to indexeddb just in case.

async loadJsonFile(filePath, hasData = false) {

if (hasData) {
  let start_time = Date.now();
  localForage.setDriver([localForage.INDEXEDDB]);
  let jsonObjects = await localForage.getItem(filePath);
  if (jsonObjects) {
    let end_time = Date.now();
    console.log ("localForage took ", (end_time - start_time), " ms");
    return jsonObjects;
  }
}

let start_time = Date.now();
const response = await fetch(filePath);
if (!response.ok) {
  return null;
}
let end_time = Date.now();
console.log ("fetch took ", (end_time - start_time), " ms ")
start_time = Date.now();
let jsonObjects = await response.json();
end_time = Date.now();
console.log ("parse took ", (end_time - start_time), " ms ")
localForage.setDriver([localForage.INDEXEDDB]);
localForage.setItem(filePath,jsonObjects);

return jsonObjects;

}

first run:
fetch took 7 ms
parse took 1047 ms

subsequence run:
localForage took 956 ms

I looked at IndexedDB in the Dev console, and it was actually stored at JSON objects (not JSON string). I wonder why it took long to retrieve data from IndexedDB?

Is there anyway to use localforage to retrieve the JSON objects faster?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant