BrowserX

A drop-in replacement for the browser gem: same Browser namespace, same require "browser", same predicates. Change one line of the Gemfile, keep every call site.

gem "browserx"

In numbers

61 µsper requestinstead of 110 µs with browser 6.2.0
129objects per requestinstead of 245 with browser 6.2.0
20,328user agents in the corpuson which BrowserX’s answers are diffed against browser’s

Measured by bench/request.rb on Ruby 4.0.9 with YJIT: what a web request asks (name, version, bot?, platform, device, accept-language), median of 7.

The benchmark, and how to reproduce it

What changes

Nothing in your code

Every predicate, every require path and the Browser namespace of browser keep working unchanged. The upstream test suite runs unmodified.

Usage

Bot detection in one pass

The bot list is matched by one prefix-trie regexp instead of one match per entry, and the browser is no longer detected twice to answer bot?.

Why it is faster

The same answers, proven

A snapshot script runs both gems over 20,328 user agents and a diff script compares them, so a speed-up cannot change a single answer unnoticed.

The corpus

Half the allocations

Platform and device matchers are built lazily, only when a request asks for them.

Benchmarks

A bot list meant to stay fresh

From permissively licensed sources: crawler-user-agents and ai.robots.txt, both MIT.

Bots

Current Ruby and Rails

Ruby 3.3 or later, tested on Rails 8.0 and 8.1. Bundler’s auto-require loads the gem, and the railtie no longer needs a preloaded ActiveSupport.

Rails

Quick start

Gemfile

gem "browserx" # instead of gem "browser"

Ruby

require "browser"

browser = Browser.new(user_agent, accept_language: "en-us")
browser.chrome?
browser.bot?
browser.device.mobile?