douyujun0152 2014-01-27 21:55
浏览 31

AJAX解析了大量的JSON并存储在localStorage中

I am building an app that should be able to work offline. I am using jQuery Mobile, PHP, MySQL and jStorage in order to easily use HTML5 localStorage

I am trying to figure out the best way to download the data into the local device localStorage and using it at later stages without slowing down or crashing the browser.

I have around 5000 records (500 Kb of data) in a MySQL table, and I need the app to download all this data so that it can be used at a second stage while offline.

download_script.php returns all the records in JSON format e.g.

{"1":{"1":{"p_bar":"10.30","v_0":"0.0312207306000000","h_vap":"311.78","p_10c":"99.99"}},"2":{"1":{"p_bar":"10.40","v_0":"0.0309405941000000","h_vap":"311.29","p_10c":"0.00"}},

I was wondering if there is any way I could optimize the following script (e.g. in order to not hang the browser, and possibly to display the percentage of data that is being downloaded)

$.ajax({
  url: "download_script.php",
  cache: false,
  success: function(big_json_dump){
  $.jStorage.set('some_key', big_json_dump);
 }
});

Can this approach be optimized e.g. using radpidjson? How can I change it so to display a live percentage of data downloaded?

  • 写回答

2条回答 默认 最新

  • duangu9666 2014-04-04 06:11
    关注

    (You had a good discussion in the comments but the question is left unanswered...)

    "in order to not hang the browser" you need to split the data into smaller parts and fetch every part separately (e.g. in a loop). Otherwise, parsing a big chunk of JSON can hang the browser for a few milliseconds. Rapidjson can't help there, because Rapidjson is C++ and the browser talks JavaScript.

    "to display the percentage of data" you need to inform the browser's JavaScript about the total amount of parts first. Again, nothing to do with Rapidjson.

    There's also JavaScript streaming JSON parsers (like Clarinet) which, theoretically, can parse the large JSON document in chunks. This gives you more control about the parsing, at the cost of much CPU and programming complexity. You'll probably need to introduce a Web Worker or an aritifial moments of inactivity to keep the browser responsive. But if you're using a Web Worker, you can do the standard JSON.parse in there: http://igorminar.github.io/webworker-json-perf/; http://blog.softwareispoetry.com/2013/05/using-web-workers-to-jsonparse-large.html

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题