0

How to update a 2MB JSON file using JavaScript?

I use phonegap to develop an Android application. Now, I need retrieve data from a JSON file and then update data back into this JSON file.

It shows me that the "JavaScript execution exceeded timeout"

  • 1
    Can we see your code? It'll help us understand exactly what's going on – SomeKittens Aug 05 '12 at 14:06
  • What does the JSON contain? Maybe JSON is not the best format if you're dealing with large amounts of data on a limited device. Have you thought about using a database like SQLite? Also, does the data come from a server? If so, it's not necessary to send the entire file to the client if it needs only a portion of the data. – Tomalak Aug 05 '12 at 14:06
  • 2
    Split the data down to smaller pieces. – rekire Aug 05 '12 at 14:06
  • Do you have any more information about the error? The file in question, the line number? – Šime Vidas Aug 05 '12 at 14:13
  • The same way you do it with XML, CSV, etc.: You parse the data, manipulate the data structure and serialise it back. – Felix Kling Aug 05 '12 at 14:22

1 Answers1

1

Most JavaScript engines have an execution limit, meaning that they will throw an error when your code runs for longer than x seconds. In most browsers, this is just 5 or 10 seconds. Have a look at this answer on SO for more information.

Three solutions come to mind:

  • Break up the long-running code in smaller pieces. Rewrite your algorithm so it works in short "spurts" and use setInterval or setTimeout to call the code in iterations.
  • Use Web Workers, which are specifically designed for long-running tasks. The downside? Browser compatibility is only so-so.
  • Run it on a server. This may be either trivial or nearly impossible, depending on your problem.
Community
  • 1
  • 1