entities vs he vs html-entities vs sanitize-html
HTML Entity Encoding Libraries Comparison
1 Year
entitieshehtml-entitiessanitize-htmlSimilar Packages:
What's HTML Entity Encoding Libraries?

HTML entity encoding libraries are essential tools in web development for handling special characters in HTML. These libraries help prevent issues such as XSS (Cross-Site Scripting) attacks by encoding or decoding HTML entities, ensuring that user input is safely rendered in web applications. They provide functionalities to convert characters into their corresponding HTML entities and vice versa, allowing developers to manage text safely and effectively in web contexts. Each library has its unique features, performance characteristics, and use cases, making it important to choose the right one based on specific project requirements.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
entities65,204,505331540 kB62 months agoBSD-2-Clause
he23,779,2923,478-236 years agoMIT
html-entities19,690,103645287 kB6a year agoMIT
sanitize-html3,080,6783,91865 kB242 months agoMIT
Feature Comparison: entities vs he vs html-entities vs sanitize-html

Encoding and Decoding

  • entities:

    The 'entities' package provides efficient encoding and decoding of HTML entities, focusing on speed and simplicity. It supports basic entity conversions and is optimized for performance, making it suitable for high-load applications.

  • he:

    The 'he' library offers extensive support for encoding and decoding HTML entities, including both named and numeric entities. It is designed to handle a wide range of character sets and is particularly useful for internationalization.

  • html-entities:

    The 'html-entities' package allows for straightforward encoding and decoding of HTML entities. It is user-friendly and provides a simple API for developers looking to handle basic entity conversions without complexity.

  • sanitize-html:

    The 'sanitize-html' library focuses on sanitizing HTML input, which includes encoding entities to prevent XSS vulnerabilities. It allows for configurable whitelisting of HTML tags and attributes, ensuring safe output.

Performance

  • entities:

    This package is optimized for performance, making it suitable for applications that require fast encoding and decoding operations, especially in high-traffic environments.

  • he:

    While 'he' is comprehensive, it may have a slightly larger footprint compared to simpler libraries. However, it remains efficient for most use cases, especially where extensive character support is needed.

  • html-entities:

    The 'html-entities' library strikes a balance between performance and usability, making it a good choice for projects that do not require extensive features but still need reliable performance.

  • sanitize-html:

    Performance can vary based on the complexity of the sanitization rules defined. It is essential to configure it properly to ensure that it performs well without compromising security.

Security Features

  • entities:

    The 'entities' package primarily focuses on encoding and decoding without built-in security features. It is best used in conjunction with other security measures to prevent XSS.

  • he:

    The 'he' library provides robust encoding and decoding capabilities, but like 'entities', it does not include specific security features. It should be used as part of a broader security strategy.

  • html-entities:

    Similar to 'entities' and 'he', 'html-entities' focuses on entity conversion without additional security features. Developers must implement their own security measures to protect against XSS.

  • sanitize-html:

    This library excels in security by sanitizing HTML input, making it a strong choice for applications that need to prevent XSS attacks. It allows developers to define safe HTML structures while encoding entities.

Ease of Use

  • entities:

    The 'entities' library is straightforward and easy to use, making it ideal for developers who need quick and efficient entity handling without a steep learning curve.

  • he:

    Despite its comprehensive feature set, 'he' maintains a user-friendly API, making it accessible for developers who need advanced entity handling without complexity.

  • html-entities:

    The 'html-entities' library is designed for ease of use, providing a simple API that allows developers to quickly implement entity encoding and decoding in their applications.

  • sanitize-html:

    While 'sanitize-html' is slightly more complex due to its sanitization capabilities, it provides clear documentation and examples, making it relatively easy to implement for developers familiar with HTML.

Community and Maintenance

  • entities:

    The 'entities' package is well-maintained and has a supportive community, ensuring that it stays updated with best practices and performance improvements.

  • he:

    The 'he' library is actively maintained and has a strong community backing, which contributes to its reliability and ongoing development.

  • html-entities:

    This package is also well-maintained, with regular updates and a community that supports its usage, ensuring it remains relevant and functional.

  • sanitize-html:

    'sanitize-html' is widely used and has a robust community, making it a reliable choice for developers concerned about ongoing support and updates.

How to Choose: entities vs he vs html-entities vs sanitize-html
  • entities:

    Choose 'entities' if you need a straightforward solution for encoding and decoding HTML entities with a focus on performance and simplicity. It is lightweight and efficient for basic use cases.

  • he:

    Select 'he' for a comprehensive library that supports a wide range of HTML entities, including those in various character sets. It is particularly useful for projects requiring extensive character support and is well-maintained.

  • html-entities:

    Opt for 'html-entities' if you prefer a library that is simple to use and provides both encoding and decoding functionalities. It is suitable for projects that require basic entity handling without additional complexity.

  • sanitize-html:

    Use 'sanitize-html' when you need to sanitize user input to prevent XSS attacks while also encoding HTML entities. This library is ideal for applications that need to allow certain HTML tags and attributes while ensuring safety.

README for entities

entities NPM version Downloads Node.js CI

Encode & decode HTML & XML entities with ease & speed.

Features

  • 😇 Tried and true: entities is used by many popular libraries; eg. htmlparser2, the official AWS SDK and commonmark use it to process HTML entities.
  • ⚡️ Fast: entities is the fastest library for decoding HTML entities (as of April 2022); see performance.
  • 🎛 Configurable: Get an output tailored for your needs. You are fine with UTF8? That'll save you some bytes. Prefer to only have ASCII characters? We can do that as well!

How to…

…install entities

npm install entities

…use entities

const entities = require("entities");

// Encoding
entities.escapeUTF8("& ü"); // "& ü"
entities.encodeXML("& ü"); // "& ü"
entities.encodeHTML("& ü"); // "& ü"

// Decoding
entities.decodeXML("asdf & ÿ ü '"); // "asdf & ÿ ü '"
entities.decodeHTML("asdf & ÿ ü '"); // "asdf & ÿ ü '"

Performance

This is how entities compares to other libraries on a very basic benchmark (see scripts/benchmark.ts, for 10,000,000 iterations; lower is better):

| Library | Version | decode perf | encode perf | escape perf | | -------------- | ------- | ------------- | ------------- | ------------- | | entities | 3.0.1 | 1.418s | 6.786s | 2.196s | | html-entities | 2.3.2 | 2.530s | 6.829s | 2.415s | | he | 1.2.0 | 5.800s | 24.237s | 3.624s | | parse-entities | 3.0.0 | 9.660s | N/A | N/A |


FAQ

What methods should I actually use to encode my documents?

If your target supports UTF-8, the escapeUTF8 method is going to be your best choice. Otherwise, use either encodeHTML or encodeXML based on whether you're dealing with an HTML or an XML document.

You can have a look at the options for the encode and decode methods to see everything you can configure.

When should I use strict decoding?

When strict decoding, entities not terminated with a semicolon will be ignored. This is helpful for decoding entities in legacy environments.

Why should I use entities instead of alternative modules?

As of April 2022, entities is a bit faster than other modules. Still, this is not a very differentiated space and other modules can catch up.

More importantly, you might already have entities in your dependency graph (as a dependency of eg. cheerio, or htmlparser2), and including it directly might not even increase your bundle size. The same is true for other entity libraries, so have a look through your node_modules directory!

Does entities support tree shaking?

Yes! entities ships as both a CommonJS and a ES module. Note that for best results, you should not use the encode and decode functions, as they wrap around a number of other functions, all of which will remain in the bundle. Instead, use the functions that you need directly.


Acknowledgements

This library wouldn't be possible without the work of these individuals. Thanks to

  • @mathiasbynens for his explanations about character encodings, and his library he, which was one of the inspirations for entities
  • @inikulin for his work on optimized tries for decoding HTML entities for the parse5 project
  • @mdevils for taking on the challenge of producing a quick entity library with his html-entities library. entities would be quite a bit slower if there wasn't any competition. Right now entities is on top, but we'll see how long that lasts!

License: BSD-2-Clause

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.

entities for enterprise

Available as part of the Tidelift Subscription

The maintainers of entities and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more.