npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

fast-typed-xml

v0.1.1

Published

A high-performance, schema-based XML parser for TypeScript with strong type safety

Readme

fast-typed-xml

npm version

A high-performance, schema-based XML parser for TypeScript with strong type safety.

Features

  • 🚀 High Performance - Significantly faster than popular alternatives
  • 🔒 Type Safety - Full TypeScript support with schema-based validation
  • 📝 Schema Definition - Define your XML structure with a simple, declarative API
  • Validation - Automatic validation with detailed error messages
  • 🎯 Zero Dependencies - Lightweight with no external dependencies

Installation

npm install fast-typed-xml

Quick Start

import * as tx from "fast-typed-xml";

// Define your schema
const bookSchema = tx.object({
    id: tx.string("id", "attribute"), 
    title: tx.string("title", "element"), 
    author: tx.string("author", "element"), 
    price: tx.number("price", "element"),
});

// Parse XML with type safety
const xml = `
  <book id="bk001">
    <title>The Great Gatsby</title>
    <author>F. Scott Fitzgerald</author>
    <price>10.99</price>
  </book>
`;

const result = bookSchema.parse(xml);
// result is fully typed!
console.log(result.title); // "The Great Gatsby"

Benchmarks

The chart compares the throughput (operations per second) of three XML parsers. fast-xml-parser achieves 3,817 ops/s, and xml2js follows with 3,402 ops/s. In contrast, fast-typed-xml reaches 28,550 ops/s (with validation) and 41,837 ops/s (parse only), which is about 7.5× faster than fast-xml-parser and 8.4× faster than xml2js in parse-only mode. This clearly shows that fast-typed-xml delivers far superior performance, providing high-speed XML parsing suitable for demanding real-time or large-scale applications.

API Reference

Exports

  • fast-typed-xml - Main schema API
  • fast-typed-xml/parser - Low-level XML parser
  • fast-typed-xml/schema - Schema types and utilities
  • fast-typed-xml/util - Utility functions

Schema API

Basic Types

string(name, type, optional?)

Parse XML element or attribute as a string.

// Element: <title>Book Title</title>
tx.string("title", "element")

// Attribute: <book id="123">
tx.string("id", "attribute")

number(name, type, optional?)

Parse XML element or attribute as a number.

// Element: <price>19.99</price>
tx.number("price", "element")

// Attribute: <item count="5">
tx.number("count", "attribute")

boolean(name, type, optional?)

Parse XML element or attribute as a boolean.

// Element: <available>true</available>
tx.boolean("available", "element")

// Attribute: <item inStock="false">
tx.boolean("inStock", "attribute")

Complex Types

object(name?, schema, optional?)

Parse XML element with nested structure.

const personSchema = tx.object({
    name: tx.string("name", "element"), 
    age: tx.number("age", "element"), 
    email: tx.string("email", "element"),
});

// Parses:
// <person>
//   <name>John Doe</name>
//   <age>30</age>
//   <email>[email protected]</email>
// </person>

array(name?, schema, optional?)

Parse multiple XML elements as an array.

// Array with element name
const genresSchema = tx.array("genre", tx.string());
// Parses: <genres><genre>Fiction</genre><genre>Drama</genre></genres>

// Array without element name (for root elements)
const booksSchema = tx.array(
    tx.object({
        title: tx.string("title", "element"),
    })
);
// Parses: <books><book><title>The Great Gatsby</title></books>

Custom Types

[!TIP]

fast-typed-xml is a library focused on type-safe XML parsing. For more robust validation, it is safer and more efficient to use dedicated validation libraries like zod, arktype, or valibot on the parsed results.

You can define custom types with validation by extending the schema API. Here's an example of creating a custom userState type:

import * as tx from "fast-typed-xml";
import { AttributeSchema, ElementSchema, ValueSchema } from "fast-typed-xml/schema";

// 1. Define your custom type
type UserState = "active" | "inactive";


// 2. Create a parser function
const parseUserState = (v: string): UserState => {
    switch (v) {
        case "active":
        case "inactive":
            return v;
        default:
            throw new Error(`Invalid user state: ${v}`);
    }
};


// 3. Define schema factories
const userStateAttributeSchema = <Optional extends boolean>(
    name: string,
    optional: Optional,
) => new AttributeSchema<UserState, Optional>(name, optional, parseUserState);

const userStateValueSchema = new ValueSchema<UserState>(parseUserState);


// 4. Create overloaded function with flexible signatures
function userState(): ValueSchema<UserState>;

function userState(
    name: string,
    kind: "attribute",
): AttributeSchema<UserState, false>;

function userState(
    name: string,
    kind: "attribute",
    optional: false,
): AttributeSchema<UserState, false>;

function userState(
    name: string,
    kind: "attribute",
    optional: true,
): AttributeSchema<UserState, true>;

function userState<Optional extends boolean>(
    name: string,
    kind: "attribute",
    optional: Optional,
): AttributeSchema<UserState, Optional>;

function userState(
    name: string,
    kind: "element",
): ElementSchema<UserState, false>;

function userState(
    name: string,
    kind: "element",
    optional: false,
): ElementSchema<UserState, false>;

function userState(
    name: string,
    kind: "element",
    optional: true,
): ElementSchema<UserState, true>;

function userState<Optional extends boolean>(
    name: string,
    kind: "element",
    optional: Optional,
): ElementSchema<UserState, Optional>;

function userState<Optional extends boolean>(
    name?: string,
    kind?: "attribute" | "element",
    optional?: Optional,
):
    | AttributeSchema<UserState, Optional>
    | ValueSchema<UserState>
    | ElementSchema<UserState, Optional> {
    if (name !== undefined) {
        switch (kind) {
            case "attribute": {
                return userStateAttributeSchema(name, optional as Optional);
            }
            case "element": {
                return new ElementSchema(
                    name,
                    userStateValueSchema,
                    optional as Optional,
                );
            }
        }
    }
    return userStateValueSchema;
}


// 5. Use your custom type in schemas
const schema = tx.object({
    state: userState("state", "element")
});
const result = schema.parse("<user><state>active</state></user>"); // result: { state: "active" }

This approach gives you:

  • Type Safety: The return type is correctly inferred based on the parameters
  • Validation: Custom parsing logic with error handling
  • Flexibility: Can be used as an element, attribute, or standalone value
  • Consistency: Same API pattern as built-in types like tx.string() and tx.number()

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.