什么是替换性能秘密? [HTML转义] [英] What is replaceAll performance secret? [HTML escape]

查看:72
本文介绍了什么是替换性能秘密? [HTML转义]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了一些时间寻找逃避html字符串的最佳方法并找到了一些讨论:讨论1 讨论2 。它引导我 replaceAll 功能。然后我进行了性能测试并试图找到达到类似速度但没有成功的解决方案:(

I spent some time looking best way to escape html string and found some discussions on that: discussion 1 discussion 2. It leads me to replaceAll function. Then I did performance tests and tried to find solution achieving similar speed with no success :(

这是我的最终测试用例集。我在网上找到它并随着我的尝试扩展(底部有4个案例)但仍然无法达到 replaceAll()表现。

Here is my final test case set. I found it on net and expand with my tries (4 cases at bottom) and still can not reach replaceAll() performance.

什么秘密女巫 replaceAll()解决方案如此迅速?

What is secret witch makes replaceAll() solution so speedy?

Greets!

代码段:

String.prototype.replaceAll = function(str1, str2, ignore) 
{
   return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
};

qwerty

目前为止最快的案例:

Fastest case so far:

html.replaceAll('&', '&amp;').replaceAll('"', '&quot;').replaceAll("'", '&#39;').replaceAll('<', '&lt;').replaceAll('>', '&gt;');


推荐答案

最后我发现了!
感谢杰克指向jsperf特定的

Finally i found it! Thanks Jack for pointing me on jsperf specific


我应该注意到测试结果很奇怪;当.replaceAll()是在Benchmark.prototype.setup中定义的
时,与全局定义(即在标记内)时相比,它的运行速度是
的两倍。
我仍​​然不确定为什么会这样,但它绝对必须与
有关jsperf本身是如何工作的。

I should note that the test results are strange; when .replaceAll() is defined inside Benchmark.prototype.setup it runs twice as fast compared to when it's defined globally (i.e. inside a tag). I'm still not sure why that is, but it definitely must be related to how jsperf itself works.

答案是:

replaceAll - 这达到jsperf限制/错误,由特殊序列<$引起c $ c>\\ $&,结果出错了。

replaceAll - this reach jsperf limit/bug, caused by special sequence "\\$&", so results was wrong.

compile() - 在没有参数的情况下调用它会将regexp定义更改为 /(?:)。我不知道它是否是bug或其他东西,但是在调用之后性能结果很糟糕。

compile() - when called with no argument it changes regexp definition to /(?:). I dont know if it is bug or something, but performance result was crappy after it was called.

这是我的结果安全测试

最后我准备了正确的测试用例

结果是,HTML转义最好的方式是使用原生DOM基于解决方案,如:

The result is, that for HTML escape best way it to use native DOM based solution, like:

document.createElement('div').appendChild(document.createTextNode(html)).parentNode.innerHTML

或者如果你重复多次,你可以用一次准备好的变量来做到这一点:

or if you repeat it many times you can do it with once prepared variables:

//prepare variables
var DOMtext = document.createTextNode("test");
var DOMnative = document.createElement("span");
DOMnative.appendChild(DOMtext);

//main work for each case
function HTMLescape(html){
  DOMtext.nodeValue = html;
  return DOMnative.innerHTML
}

谢谢大家的合作&发表评论和指示。

Thank you all for collaboration & posting comments and directions.

jsperf错误描述

jsperf bug description

String.prototype.replaceAll 定义如下:

function (str1, str2, ignore) {
  return this.replace(new RegExp(str1.replace(repAll, "\\#{setup}"), (ignore ? "gi" : "g")), (typeof(str2) == "string") ? str2.replace(/\$/g, "$$") : str2);
}

这篇关于什么是替换性能秘密? [HTML转义]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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