Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Escape HTML string tags in JavaScript
(version: 1)
Escape HTML string tags in JavaScript by Character replacement vs. Tagged template literal function
Comparing performance of:
escapeHtmlByReplacingCharacters vs escapeHtml
Created:
10 months ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
function escapeHtmlByReplacingCharacters(str) { if (typeof str !== 'string') { return ''; } const escapeCharacter = (match) => { switch (match) { case '&': return '&'; case '<': return '<'; case '>': return '>'; case '"': return '"'; case '\'': return '''; case '`': return '`'; default: return match; } }; return str.replace(/[&<>"'`]/g, escapeCharacter); } function escapeHtml(strings, ...arguments) { const div = document.createElement('div'); let output = strings[0]; const args = arguments.entries(); for (const [i, arg] of args) { div.innerText = arg; output += div.innerHTML; output += strings[i + 1]; } return output; }
Tests:
escapeHtmlByReplacingCharacters
const testString = "Hello, <b>world</b>! & 'this' is a \"test\" string with `backticks`."; escapeHtmlByReplacingCharacters(testString);
escapeHtml
const testString = "Hello, <b>world</b>! & 'this' is a \"test\" string with `backticks`."; escapeHtml`${testString}`
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
escapeHtmlByReplacingCharacters
escapeHtml
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:149.0) Gecko/20100101 Firefox/149.0
Browser/OS:
Firefox 149 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
escapeHtmlByReplacingCharacters
2609434.5 Ops/sec
escapeHtml
1249312.2 Ops/sec
Related benchmarks:
Capitalize first char of a string
Capitalize first char of a string 2
stringify by concatenating or regex replacement or array joining
rest params vs arguments (rest params are fast)
replaceAll vs. adding characters up
escape and escapeAttr
escape and escapeAttr and escapeLoop 2
regex vs filter 2
replaceAll vs regex replace vs neu parser
Comments
Confirm delete:
Do you really want to delete benchmark?