duanpi7578 2017-12-05 23:12
浏览 110

JSON解析 - 内存依赖?

I'm trying to pass very large arrays to JavaScipt to be manipulated client-side, and it's not working. These arrays typically have about 12,000 elements each, and this snippet of code is functioning correctly when I only plug in 2,000 elements.

    <script type="text/javascript">

        var getLenders = JSON.parse('<?php echo json_encode($getlenders); ?>');
        var discountArray = JSON.parse('<?php echo json_encode($discountArray); ?>');
        var getData = JSON.parse('<?php echo json_encode($sortedArray); ?>');

    </script>

I increased the memory requirements of the server to 512MB (4 time as much as before) thinking that'd be more than enough, however it is still not working. Before I crank up the memory to 1024MB, I would like to know if there is any other reason this is not working. Could it be an issue with client-side memory?

Any input is appreciated.

  • 写回答

3条回答 默认 最新

  • dongqing7789 2017-12-05 23:20
    关注

    I'm sure this not good idea truly, Because you can't know you client has enough memory to run your page or not and this page freeze the browser or crash it.

    first you can optimize this code to:

        var getLenders = <?php $a =  json_encode($getlenders);  echo $a == null? '{}': $a; ?> ;
        var discountArray = <?php $b =  json_encode($discountArray); echo $b == null? '{}': $b;  ?>;
        var getData = <?php $c = json_encode($sortedArray); echo $c == null? '{}': $c; ?>;
    

    because json based on JavaScript and not need convert to string and parse again.

    Second but I have one idea: get only needed value with ajax when you need them.

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置