比较JavaScript中的大字符串和哈希 [英] Comparing large strings in JavaScript with a hash

查看:75
本文介绍了比较JavaScript中的大字符串和哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有文本区域的表单,该表单可以包含使用许多第三方富文本编辑器之一进行编辑的大量内容(例如,博客文章).我正在尝试实现类似自动保存功能的功能,如果更改了内容,该功能应通过ajax提交内容.但是,我必须解决以下事实:我作为选项使用的某些编辑器不支持脏"标志或"onchange"事件,我可以使用该事件查看自上次保存以来内容是否已更改.

I have a form with a textarea that can contain large amounts of content (say, articles for a blog) edited using one of a number of third party rich text editors. I'm trying to implement something like an autosave feature, which should submit the content through ajax if it's changed. However, I have to work around the fact that some of the editors I have as options don't support an "isdirty" flag, or an "onchange" event which I can use to see if the content has changed since the last save.

因此,作为一种解决方法,我想在最后一次保存时将内容的副本保存在变量中(我们将其称为lastSaveContent),并在自动保存"时将其与当前文本进行比较. "功能(在计时器上触发)以查看其是否不同.但是,我担心非常大的文档会占用多少内存.

So, as a workaround, what I'd like to do is keep a copy of the content in a variable (let's call it lastSaveContent), as of the last save, and compare it with the current text when the "autosave" function fires (on a timer) to see if it's different. However, I'm worried about how much memory that could take up with very large documents.

将某种哈希存储在lastSaveContent变量中而不是整个字符串中,然后比较哈希值会更有效吗?如果是这样,您可以推荐一个好的javascript库/jquery插件来实现此要求的适当哈希吗?

Would it be more efficient to store some sort of hash in the lastSaveContent variable, instead of the entire string, and then compare the hash values? If so, can you recommend a good javascript library/jquery plugin that implements an appropriate hash for this requirement?

推荐答案

简而言之,最好只存储和比较两个字符串.

In short, you're better off just storing and comparing the two strings.

计算适当的哈希值很便宜.例如,查看伪代码

Computing a proper hash is not cheap. For example, check out the pseudo code or an actual JavaScript implementation for computing the MD5 hash of a string. Furthermore, all proper hash implementations will require enumerating the characters of the string anyway.

此外,在现代计算的背景下,必须先将字符串与其他字符串进行比较,然后再进行比较.您在这里所做的实际上是微优化.内存不会成为问题,CPU也不会循环比较两个字符串.

Furthermore, in the context of modern computing, a string has to be really, really long before comparing it against another string is slow. What you're doing here is effectively a micro-optimization. Memory won't be an issue, nor will the CPU cycles to compare the two strings.

与所有优化案例一样,请先检查,这实际上是一个问题,然后再解决它.在我进行的一项快速测试中,计算和比较2个MD5和耗时382ms.比较这两个字符串直接花费了0ms.这使用的字符串长度为10000个单词.参见 http://jsfiddle.net/DjM8S .

As with all cases of optimizing: check that this is actually a problem before you solve it. In a quick test I did, computing and comparing 2 MD5 sums took 382ms. Comparing the two strings directly took 0ms. This was using a string that was 10000 words long. See http://jsfiddle.net/DjM8S.

如果您真的将其视为一个问题,我也将强烈考虑使用穷人比较;然后只比较两个字符串的长度,以查看它们是否已更改,而不是实际的字符串比较.

If you really see this as an issue, I would also strongly consider using a poor-mans comparison; and just comparing the length of the 2 strings, to see if they have changed or not, rather than actual string comparisons.

..

这篇关于比较JavaScript中的大字符串和哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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