Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Escape HTML string tags in JavaScript
Escape HTML string tags in JavaScript by Character replacement vs. Tagged template literal function
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Android 16; Mobile; rv:144.0) Gecko/144.0 Firefox/144.0
Browser:
Firefox Mobile 144
Operating system:
Android
Device Platform:
Mobile
Date tested:
6 months ago
Test name
Executions per second
escapeHtmlByReplacingCharacters
1097281.4 Ops/sec
escapeHtml
328500.0 Ops/sec
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}`