Code Monkey home page Code Monkey logo

libxml2-wasm's Introduction

libxml2-wasm

CI Build on master npm

Why another xml lib?

Comparing to the previous two main approaches, pure javascript implementation as well as traditional C implementation binding, using WebAssembly combines the pros from both sides, providing good performance while keeping best compatibility with modern Javascript runtime.

Javascript Implementation Traditional C Binding WebAssembly
Parsing Speed1 Average Fast Fast
C/C++ Toolchain Not required Required2 Not Required
Prebuilt Binaries N/A One for each OS/Runtime version Universal for all OS/Runtime versions
Prebuilt Binary Compatibility N/A May broke across libc versions Very Good
Browser Compatibility Yes No Yes

Documentation

https://jameslan.github.io/libxml2-wasm/index.html

Supported Environments

Due to the usage of WebAssembly, ES module and top level await etc, it requires the minimum version of the following environments,

Environment Version
NodeJs v16+
Chrome V89+
Edge V89+
Safari v15+

Getting started

Install libxml2-wasm package:

npm i libxml2-wasm

libxml2-wasm is an ES module, importing it are different between ES module and commonJS module.

ESM

Import it directly.

import fs from 'node:fs';
import { XmlDocument } from 'libxml2-wasm';
const doc1 = XmlDocument.fromString('<note><to>Tove</to></note>');
const doc2 = XmlDocument.fromBuffer(fs.readFileSync('doc.xml'));
doc1.dispose();
doc2.dispose();

CommonJS

Dynamic import is needed:

const fs = require('node:fs');
import('libxml2-wasm').then(({ XmlDocument }) => {
    const doc1 = XmlDocument.fromString('<note><to>Tove</to></note>');
    const doc2 = XmlDocument.fromBuffer(fs.readFileSync('doc.xml'));
    doc1.dispose();
    doc2.dispose();
});

IMPORTANT: dispose() is required to avoid memory leak.

For more detail, see the Doc.

Footnotes

  1. The speed of different libraries varies a lot, see benchmark.

  2. The requirement of C/C++ toolchain may be waived if prebuilt binary is available.

libxml2-wasm's People

Contributors

jameslan avatar fennibay avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.