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

HTML entity encoding libraries are essential tools in web development that help manage the conversion of characters into their corresponding HTML entities. This is particularly important for ensuring that special characters are displayed correctly in web browsers and to prevent issues such as XSS (Cross-Site Scripting) attacks. These libraries provide functionalities to encode and decode HTML entities, making it easier for developers to handle user-generated content safely and effectively. Each library has its unique features, performance characteristics, and use cases, allowing developers to choose the most suitable one for their needs.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
entities66,005,468338540 kB65 months agoBSD-2-Clause
he22,664,4453,497-247 years agoMIT
html-entities18,438,010656132 kB4a month agoMIT
Feature Comparison: entities vs he vs html-entities

Encoding Support

  • entities:

    The 'entities' library supports basic HTML entity encoding and decoding, focusing on performance and simplicity. It covers common entities but may not support all edge cases or less common entities.

  • he:

    The 'he' library provides extensive support for HTML5 entities, including named and numeric entities. It is well-suited for applications that require comprehensive encoding capabilities and can handle a wide variety of characters.

  • html-entities:

    The 'html-entities' library supports both HTML and XML entities, making it versatile for different use cases. It includes a range of predefined entities and allows for easy encoding and decoding.

Performance

  • entities:

    Designed for speed, 'entities' is optimized for performance, making it suitable for applications where encoding and decoding speed is critical, such as in high-traffic environments.

  • he:

    While 'he' is slightly larger in size due to its extensive feature set, it still maintains good performance for most use cases. It is efficient for applications that require robust encoding without significant performance drawbacks.

  • html-entities:

    The 'html-entities' library strikes a balance between performance and functionality, making it suitable for a wide range of applications without sacrificing speed.

Ease of Use

  • entities:

    The 'entities' library offers a simple and intuitive API, making it easy for developers to implement encoding and decoding with minimal setup and configuration.

  • he:

    'he' provides a more feature-rich API, which may require a bit more understanding but offers greater flexibility for complex encoding scenarios.

  • html-entities:

    The 'html-entities' library is user-friendly, with straightforward methods for encoding and decoding, making it accessible for developers of all skill levels.

Community and Maintenance

  • entities:

    The 'entities' library is maintained with a focus on simplicity and performance, but it may not have as large a community or extensive documentation compared to others.

  • he:

    'he' has a strong community and is actively maintained, with regular updates and improvements, making it a reliable choice for long-term projects.

  • html-entities:

    The 'html-entities' library is also well-maintained and has a decent community, providing good support and documentation for developers.

Use Cases

  • entities:

    Best suited for lightweight applications or scenarios where basic HTML entity encoding is required without additional overhead.

  • he:

    Ideal for applications that need to handle a wide variety of character encodings, especially those that deal with internationalization or complex text inputs.

  • html-entities:

    Great for projects that require both HTML and XML entity handling, providing flexibility for different content types.

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

    Choose 'entities' if you need a lightweight library that provides a straightforward API for encoding and decoding HTML entities, with a focus on performance and simplicity. It is suitable for small projects or when you need minimal overhead.

  • he:

    Opt for 'he' if you require a comprehensive solution that supports a wide range of character encodings, including HTML5 entities. It is ideal for applications that need robust handling of various character sets and extensive encoding options.

  • html-entities:

    Select 'html-entities' if you want a library that offers a balance between functionality and ease of use, with support for both HTML and XML entities. It is a good choice for projects that require flexibility in handling different types of entities.

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.