JSON.stringify 对于大对象来说非常慢 [英] JSON.stringify is very slow for large objects

查看:78
本文介绍了JSON.stringify 对于大对象来说非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JavaScript 中有一个非常大的对象(大约 10MB).

I have a very big object in javascript (about 10MB).

当我对它进行字符串化时,它需要很长时间,所以我将它发送到后端并将其解析为一个对象(实际上是带有数组的嵌套对象),这也需要很长时间,但这不是我们在这个问题中的问题.

And when I stringify it, it takes a long time, so I send it to backend and parse it to an object( actually nested objects with arrays), and that takes long time too but it's not our problem in this question.

问题:

如何使 JSON.stringify 更快,任何想法或替代方案,我需要一个 javaScript 解决方案、我可以使用的库或这里的想法.

How can I make JSON.stringify faster, any ideas or alternatives, I need a javaScript solution, libraries I can use or ideas here.

我的尝试

我用谷歌搜索了很多,看起来没有比 JSON.stringify 更好的性能了,或者我的谷歌搜索技能生疏了!

I googled a lot and looks there is no better performance than JSON.stringify or my googling skills got rusty!

结果

我接受任何可以解决请求中长时间保存(发送到后端)的建议(我知道它的大请求).

I accept any suggestion that may solve me the long saving (sending to backend) in the request (I know its big request).

问题代码示例(问题详情)

Request URL:http://localhost:8081/systemName/controllerA/update.html;jsessionid=FB3848B6C0F4AD9873EA12DBE61E6008
Request Method:POST
Status Code:200 OK

我正在向后端发送 POST,然后在 JAVA

Am sending a POST to backend and then in JAVA

request.getParameter("BigPostParameter")

request.getParameter("BigPostParameter")

我阅读它以使用

 public boolean fromJSON(String string) {
        if (string != null && !string.isEmpty()) {
            ObjectMapper json = new ObjectMapper();
            DateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD_T_HH_MM_SS_SSS_Z);
            dateFormat.setTimeZone(TimeZone.getDefault());
            json.setDateFormat(dateFormat);
            json.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
            WebObject object;
//            Logger.getLogger("JSON Tracker").log(Level.SEVERE, "Start");
            try {
                object = json.readValue(string, this.getClass());
            } catch (IOException ex) {
                Logger.getLogger(JSON_ERROR).log(Level.SEVERE, "JSON Error: {0}", ex.getMessage());
                return false;
            }
//            Logger.getLogger("JSON Tracker").log(Level.SEVERE, "END");
            return this.setThis(object);
        }
        return false;
    }

喜欢这个

BigObject someObj = new BigObject();
someObj.fromJSON(request.getParameter("BigPostParameter"))

P.S : 仅供参考这一行 object = json.readValue(string, this.getClass());也非常非常非常慢.

P.S : FYI this line object = json.readValue(string, this.getClass()); is also very very very slow.

再次总结

  • 发布时间问题(字符串化)JavaScript 瓶口.

  • Problem in posting time (stringify) JavaScript bottle nick.

另一个问题是将字符串化为对象(使用jackson),主要是我在字符串化对象中将svg标签内容作为样式列,其他列是字符串,主要是int

Another problem parsing that stringified into an object (using jackson), and mainly I have svg tags content in that stringified object as a style column, and other columns are strings, int mainly

推荐答案

正如评论者所说 - 没有办法让解析更快.

As commenters said - there is no way to make parsing faster.

如果担心应用程序在字符串化/解析时被阻止,那么尝试将数据拆分为单独的对象,将它们字符串化并组装回一个对象,然后再保存到服务器上.

If the concern is that the app is blocked while it's stringifying/parsing then try to split data into separate objects, stringily them and assemble back into one object before saving on the server.

如果应用的加载时间不是问题,您可以尝试在现有应用的基础上进行临时增量更改.

If loading time of the app is not a problem you could try to ad-hoc incremental change on top of the existing app.

  • ... 应用加载
  • 加载地图数据
  • 制作完整的数据副本
  • ...结束加载
  • ... 应用程序无需更改即可运行
  • ...保存更改时
  • 使用更改的数据进行差异复制以获得 JSON 差异
  • 发送更改(比完整数据小得多)
  • ...在服务器上
  • 将服务器上的 JSON 差异更改应用于服务器上存储的完整数据
  • 保存更改的数据

我使用了 json-diff https://github.com/andreyvit/json-diff 计算变化,并且几乎没有类似物.

I used json-diff https://github.com/andreyvit/json-diff to calc changes, and there are few analogs.

这篇关于JSON.stringify 对于大对象来说非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆