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 🙏

© 2024 – Pkg Stats / Ryan Hefner

trie-uaparser

v1.0.5

Published

Yet another user agent parser

Downloads

6

Readme

Yet another user agent parser

As of time of writing (3rd june 2018), the fastest user agent parser.

$ node benchmark/benchmark-parse.js
   trie-uaparser v1.0.5 x 106,078 ops/sec ±0.82% (92 runs sampled)
       useragent v2.3.0 x 9,442 ops/sec ±1.10% (88 runs sampled)
useragent_parser v1.0.0 x 59,402 ops/sec ±0.98% (91 runs sampled)
useragent-parser v0.1.1 x 21,017 ops/sec ±1.39% (87 runs sampled)
       ua-parser v0.3.5 x 41,224 ops/sec ±1.33% (89 runs sampled)
Fastest is trie-uaparser v1.0.5

Examples

const {parse} = require("trie-uaparser");

const ua = "Mozilla/5.0 (Linux; U; Android 4.1.2; fr-fr; G740-L00 Build/HuaweiG740-L00) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30";
expect(parse(ua)).to.equal("Android Browser");

const match = parse(ua, {
    match: true
});

expect(match).not.to.equal(false);
expect(match.family).to.equal("Android Browser");
expect(match.version).to.equal("4.0");
expect(match.os).not.to.equal(null);
expect(typeof match.os).to.equal("object");
expect(match.os.family).to.equal("Android");
expect(match.os.version).to.equal("4.1.2");

Differences with existing user agent parsers

Most of the user agents parser use multiple regular expressions, each of them associated to a particular software/os.

This library instead, split the user agent into tokens and determines the software/os using a trie data structure.

It leads to:

Speed

Search using trie data structure will always be faster than looping through regexes execution. And if you have lots of data to analyse, speed is not only a nice thing to have.

Evolution

Adding a new pattern does not conflict with existing patterns.

In regexes based parsers, to detect Edge in "Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 11) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299", /edge/ has to be tested before /chrome/ to detect Safari without conflicting with Edge, /safari/ has to be between /chrome/ and /edge/

The more patterns you add, the harder it is to make it play well with existing patterns.

More over, adding a new pattern has a small impact on speed because it is similar to add an entry in a hashmap which is not the case for regexes based parsers.