<script src="https://cdnjs.cloudflare.com/ajax/libs/sanitize-html/1.27.5/sanitize-html.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dompurify/3.0.9/purify.min.js"></script>
const testString = `
<b>Welcome to safeland</b><br>
<a href='javascript:alert(1)'>This is fun</a><br>
<img src=x onerror=console.log(1)>
`
const result = DOMPurify.sanitize(testString)
const testString = `
<b>Welcome to safeland</b><br>
<a href='javascript:alert(1)'>This is fun</a><br>
<img src=x onerror=console.log(1)>
`
const result = sanitizeHtml(testString)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
DOMPurify | |
Sanitize HTML |
Test name | Executions per second |
---|---|
DOMPurify | 40986.0 Ops/sec |
Sanitize HTML | 191538.1 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is comparing two HTML sanitization libraries: sanitize-html
(also known as Sanitize HTML) and DOMPurify
. The goal is to measure which library performs better in terms of speed.
Options Compared
There are only two options being compared:
Pros and Cons
Here are the pros and cons of each approach:
Sanitize HTML:
Pros:
Cons:
DOMPurify:
Pros:
Cons:
Other Considerations
Both libraries are designed to work with JavaScript, but they have different implementation details. For example, DOMPurify uses a more aggressive approach to sanitizing HTML, which may impact performance.
Library Used in Test Case
In the test case, both sanitize-html
and DOMPurify
libraries are used. The Sanitize HTML library is included in the HTML preparation code, while the DOMPurify library is referenced in the JavaScript preparation code using a script tag.
Special JS Feature or Syntax
The test cases use a special syntax to create a malicious HTML string that exploits vulnerabilities in the sanitization process. This is done to simulate real-world XSS attacks and measure which library can handle them better.
In summary, this benchmark compares two popular HTML sanitization libraries: Sanitize HTML and DOMPurify. The results provide insights into which library performs better in terms of speed and security features.
Other Alternatives
If you're interested in exploring alternative options, here are a few notable ones:
Keep in mind that each alternative has its own strengths and weaknesses, and the choice of which one to use depends on your specific requirements and constraints.