You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I found that (in chrome) using string.replace is faster than string.split.join. Also performance can be boosted by caching regex objects used in string.replace once they are created. Here are some benchmarks. Regards.
var str = '& &<some html="string">html for the win</html>'
var r1 = /&(?!\w+;)/g;
var r2 = /</g;
var r3 = />/g;
var r4 = /"/g;
var r5 = /'/g;
var t = (new Date()).valueOf();
for(var i = 0; i < 100000; ++i) {
var rr = str.replace(/&(?!\w+;)/g, '&');
}
console.log('String.replace with new regexp every time', (new Date()).valueOf() - t);
var t = (new Date()).valueOf();
for(var i = 0; i < 100000; ++i) {
var rr = str.replace(r1, '&');
}
console.log('String.replace reusing regexp', (new Date()).valueOf() - t);
var t = (new Date()).valueOf();
for(var i = 0; i < 100000; ++i) {
var rr = str.split('<').join('<').split('>').join('>')
.split('"').join('"').split("'").join(''');
}
console.log('String.split.join', (new Date()).valueOf() - t);
var t = (new Date()).valueOf();
for(var i = 0; i < 100000; ++i) {
var rr = str.replace(r2, '<').replace(r3, '>').replace(r4, '"').replace(r5, ''');
}
console.log('String.replace (reusing regexps)', (new Date()).valueOf() - t);
The text was updated successfully, but these errors were encountered:
I found that (in chrome) using string.replace is faster than string.split.join. Also performance can be boosted by caching regex objects used in string.replace once they are created. Here are some benchmarks. Regards.
The text was updated successfully, but these errors were encountered: