简体   繁体   English

使用javascript创建和解析大字符串?

[英]Creating and parsing huge strings with javascript?

I have a simple piece of data that I'm storing on a server, as a plain string. 我有一个简单的数据存储在服务器上,作为一个普通的字符串。 It is kind of ridiculous, but it looks like this: 这有点荒谬,但它看起来像这样:

name|date|grade|description|name|date|grade|description|repeat for a long time

this string can be up to 1.4mb in size. 这个字符串的大小最多可达1.4mb。 The idea is that it's a bunch of student records, just strung together with a simple pipe delimeter. 这个想法是,它是一堆学生记录,只是简单的管道分隔线。 It's a very poor serialization method. 这是一个非常差的序列化方法。

Once this massive string is pushed to the client, it is split along the pipes into student records again, using javascript. 一旦将这个庞大的字符串推送到客户端,它就会使用javascript再次沿着管道分成学生记录。

I've been timing how long it takes to create, and split, these strings on the client side. 我已经计算了在客户端创建和拆分这些字符串需要多长时间。 The times are actually quite good, the slowest run I've seen on a few different machines is 0.2 seconds for 10,000 'student records', which has a final string size of ~1.4mb. 时间实际上相当不错,我在几台不同的机器上看到的最慢的运行时间为10,000秒“学生记录”的0.2秒,其最终字符串大小约为1.4mb。

I realize this is quite bizarre, just wondering if there are any inherent problems with creating and splitting such large strings using javascript? 我意识到这很奇怪,只是想知道使用javascript创建和拆分这么大的字符串是否有任何固有的问题? I don't know how different browsers implement their javascript engines. 我不知道不同的浏览器如何实现他们的javascript引擎。 I've tried this on the 'major' browsers, but don't know how this would perform on earlier versions of each. 我在'主要'浏览器上试过这个,但是不知道它会如何在每个版本的早期版本上执行。

Yeah looking for any comments on this, this is more for fun than anything else! 是的,寻找任何评论,这比其他任何东西更有趣!

Thanks 谢谢

String splitting for 1.4mb data is not a problem for decent machines, instead you should worry about the internet connection speed of your users. 1.4mb数据的字符串拆分对于不错的机器来说不是问题,相反,你应该担心用户的互联网连接速度。 I've tried to do spell check with 800 kb dictionary (which is half of your data), main issue was loading time. 我试图用800 kb字典(这是你的数据的一半)进行拼写检查,主要问题是加载时间。

But looks like your students records data could be put in database, and might not need to load everything at loading time, So, how about do a pagination to show user records or use ajax to request to search certain user names? 但看起来您的学生记录数据可能会被放入数据库中,并且可能不需要在加载时加载所有内容,那么,如何分页以显示用户记录或使用ajax来请求搜索某些用户名?

If it's a really large string it may pay to continuously slice the string with 'string'.slice(from, to) to only process a smaller subset, appending all of the individual items to the end of the output with list.push() or something similar might work. 如果它是一个非常大的字符串,可能需要用'string'.slice(from, to)连续切片,只处理一个较小的子集,用list.push()将所有单个项追加到输出的末尾。或类似的东西可能有效。

String split methods are probably the most efficient way of doing this though, even in IE. 尽管在IE中,字符串拆分方法可能是最有效的方法。 Processing individual characters using string.charAt(x) is extremely slow and will often show a security error as it stalls the browser. 使用string.charAt(x)处理单个字符非常慢,并且在停止浏览器时经常会出现安全错误。 Using string split methods would certainly be much faster than splitting using regular expressions. 使用字符串拆分方法肯定比使用正则表达式拆分快得多。

It may also be possible to encode the data using a JSON array, some newer browsers such as IE8/Webkit/FF3.5 have fast JSON parsing built in using JSON.parse(data) . 也可以使用JSON数组对数据进行编码,一些较新的浏览器(如IE8 / Webkit / FF3.5 JSON.parse(data)使用JSON.parse(data)内置快速JSON解析。 But using eval(JSON) may overflow the browser if there's enough data, so is probably a bad idea. 但是如果有足够的数据,使用eval(JSON)可能会溢出浏览器,因此可能是一个坏主意。 It may pay to compare for performance though. 虽然可以比较性能。

A much better approach in a lot of cases is to use AJAX and only load some of the data at once from the server, which would also save download time. 在很多情况下,更好的方法是使用AJAX并仅从服务器一次加载一些数据,这也可以节省下载时间。

Besides S. Mark's excellent comments about local vs. x-fer speed and the tip to re-encode using AJAX, I suggest a (longterm) move away from JavaScript in the Browser ( assuming that's were it runs ) to either a non-browser implementation of JS (or possibly another language). 除了S. Mark关于本地与x-fer速度的优秀评论以及使用AJAX重新编码的提示之外,我建议(长期)从浏览器中的JavaScript( 假设它是运行的 )转移到非浏览器JS(或可能是另一种语言)的实现。

A browser based JS seems a week link in a data-x-fer chain and nothing I would want to run unmonitored, since the browsers are upgraded from time to time and breaking your JS-x-fer might be an unanticipates side effect! 基于浏览器的JS似乎是数据-x-fer链中的一周链接,而我想要不受监控的任何东西,因为浏览器不时升级并且破坏你的JS-x-fer可能是一个意想不到的副作用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM