Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sanitize HTML String - DomParser vs Regex
(version: 0)
Comparing performance of:
DomParser vs Regex
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
DomParser
var testStr = `Nome: Tênis Puma Cell Active BDP Masculino<br>Gênero: Masculino<br>Peso do Produto: 322 g (O peso do produto varia de acordo com a numeração)<br><br>Garantia do Fabricante: Contra defeito de fabricação.<br>Origem: Nacional`; var parser = new DOMParser(); var parsed = parser.parseFromString(testStr, 'text/html'); var text = parsed.body.textContent;
Regex
var testStr = `Nome: Tênis Puma Cell Active BDP Masculino<br>Gênero: Masculino<br>Peso do Produto: 322 g (O peso do produto varia de acordo com a numeração)<br><br>Garantia do Fabricante: Contra defeito de fabricação.<br>Origem: Nacional`; var text = testStr.replace(/<[^>]*>/g, '')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
DomParser
Regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0
Browser/OS:
Firefox 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
DomParser
42806.5 Ops/sec
Regex
1635791.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to compare two approaches for sanitizing an HTML string: 1. Using the `DOMParser` API 2. Using regular expressions (`Regex`) The goal is to measure which approach is faster for this specific use case. **Options Compared** The two options are compared in terms of their execution speed. The benchmark aims to determine which method is faster, as measured by the number of executions per second on a given device platform (Desktop) and browser version (Firefox 130). **Pros and Cons of Each Approach** 1. **DOMParser API** * Pros: + More readable and maintainable code + Better suited for HTML parsing and manipulation * Cons: + May be slower than regular expressions for simple string sanitization + Requires more overhead due to the creation of a temporary DOM document 2. **Regular Expressions (`Regex`)** Pros: * Fastest approach for simple string sanitization * Compact and efficient code Cons: * May not be as readable or maintainable for complex patterns + Can be error-prone if not used correctly **Library Used (if applicable)** In this case, the `DOMParser` API is a built-in JavaScript library that's part of the browser's core functionality. **Special JS Feature or Syntax** The benchmark uses template literals (`var testStr = \`...'\`) to define the HTML string. This is a relatively modern feature in JavaScript that allows for more readable and efficient string interpolation. It's not essential to know this syntax to understand the benchmark, but it does contribute to the overall readability of the code. **Other Alternatives** If `DOMParser` or regular expressions aren't suitable for your use case, other alternatives might include: * Using a dedicated HTML parsing library like `cheerio` * Employing a custom string manipulation function * Utilizing a different approach, such as using an XML parser (if the input is always well-formed XML)
Related benchmarks:
RegEx.test vs. String.includes vs. String.match insensitive
RegEx.test (various styles) vs. String.includes vs. String.match vs. String Equality vs. String.indexOf
RegEx.test vs. String.includes incasesensitive
RegEx.test vs. Inline RegEx.test
Comments
Confirm delete:
Do you really want to delete benchmark?