Created
November 23, 2016 19:09
-
-
Save leeoniya/f8e8930bce61a268a15f70e658bde0d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Benchmark = require('benchmark'); | |
function control(_string) { | |
const string = _string + ''; | |
const length = string.length; | |
let characters = ''; | |
for (let i = 0; i < length; i++) { | |
switch (string.charCodeAt(i)) { | |
case 38: | |
characters += '&'; | |
break; | |
case 39: | |
characters += '''; | |
break; | |
case 34: | |
characters += '"'; | |
break; | |
case 60: | |
characters += '<'; | |
break; | |
case 62: | |
characters += '>'; | |
break; | |
default: | |
characters += string[i]; | |
} | |
} | |
return characters; | |
} | |
var UNSAFE_CHARS_PATTERN = /[<>&"']/g; | |
// Mapping of unsafe HTML and invalid JavaScript line terminator chars to | |
// their Unicode counterparts which are safe to use in JavaScript strings. | |
var UNICODE_CHARS = { | |
'<' : '<', | |
'>' : '>', | |
'"' : '"', | |
"'" : ''', | |
'&' : '&' | |
}; | |
function treatment(_str) { | |
const str = _str + ''; | |
return str.replace(UNSAFE_CHARS_PATTERN, function(unsafeChar) { | |
return UNICODE_CHARS[unsafeChar]; | |
}); | |
} | |
function escape(s) { | |
s = ''+s; | |
const len = s.length; | |
for (var i = 0, out = ''; i < len; i++) { | |
switch (s[i]) { | |
case '&': out += '&'; break; | |
case '<': out += '<'; break; | |
case '>': out += '>'; break; | |
case '"': out += '"'; break; | |
case "'": out += '''; break; | |
case '/': out += '/'; break; | |
default: out += s[i]; | |
} | |
} | |
return out; | |
} | |
const shortStr = 'asdf'; | |
const longStr = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam ullamcorper efficitur dolor imperdiet tristique. Mauris a condimentum tortor, quis bibendum ante. Cras purus massa, molestie vitae tristique nec, mollis ut justo. Nulla faucibus convallis diam, sed finibus diam eleifend ac. Aliquam sed sodales enim. Nulla volutpat fringilla eros, ac consectetur eros tempor id. Proin diam nisi, rutrum non luctus in, vestibulum vel ex.'; | |
const shortHtml = '<foo>bar</foo>'; | |
const longHtml = '<div><span>other component!</span><div><div><span>other component!</span><div><div><span>other component!</span><div></div></div></div></div></div></div>'; | |
(new Benchmark.Suite) | |
.add('control(shortStr)', function() { | |
control(shortStr); | |
}) | |
.add('treatment(shortStr)', function() { | |
treatment(shortStr); | |
}) | |
.add('escape(shortStr)', function() { | |
escape(shortStr); | |
}) | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name') + "\n"); | |
}) | |
.run({ 'async': false }); | |
(new Benchmark.Suite) | |
.add('control(longStr)', function() { | |
control(longStr); | |
}) | |
.add('treatment(longStr)', function() { | |
treatment(longStr); | |
}) | |
.add('escape(longStr)', function() { | |
escape(longStr); | |
}) | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name') + "\n"); | |
}) | |
.run({ 'async': false }); | |
(new Benchmark.Suite) | |
.add('control(shortHtml)', function() { | |
control(shortHtml); | |
}) | |
.add('treatment(shortHtml)', function() { | |
treatment(shortHtml); | |
}) | |
.add('escape(shortHtml)', function() { | |
escape(shortHtml); | |
}) | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name') + "\n"); | |
}) | |
.run({ 'async': false }); | |
(new Benchmark.Suite) | |
.add('control(longHtml)', function() { | |
control(longHtml); | |
}) | |
.add('treatment(longHtml)', function() { | |
treatment(longHtml); | |
}) | |
.add('escape(longHtml)', function() { | |
escape(longHtml); | |
}) | |
.on('cycle', function(event) { | |
console.log(String(event.target)); | |
}) | |
.on('complete', function() { | |
console.log('Fastest is ' + this.filter('fastest').map('name') + "\n"); | |
}) | |
.run({ 'async': false }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment