Skip to content

bgalek/devicedetect

Repository files navigation

Simple, fast and 0 dependencies library for device detection.

GitHub Workflow Status Codecov GitHub Release Date Scrutinizer code quality Quality Gate Status

Why?

When comes to user agent parsing and normalization, sometimes few simple regex matches are enough good. Just like in varnish-devicedetect.

This java library is an adaptation of this regular expressions list. Test cases origin from a varnish webserver with varnish-devicedetect installed. This library is almost 100% consistent (a little more things are considered as bot).

Accuracy

Few hundred thousands of requests were checked against varnish-devicedetect and yauua. There was about 0.3% of differences.

Maybe it's enough for You too!

Feel free to explore library test data.

Performance

Other libraries try to be as much accurate as possible. They extract more information from User-Agent header and they use clever tricks to be performant. Most of them are decent piece of software. But sometimes, a much simpler approach will be acceptable.

Usage

Add library dependency:

compile "com.github.bgalek.utils:devicedetect:1.0.0"

Detect using default detectors:

DeviceDetect.basic().detect("Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36")

Provide custom detections:

DeviceDetect.custom().withCustomDetectors(List.of()).detect("Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36")

Performance

Library is considered thread-safe.

JMH benchmark is included in project.

That's the result:

Benchmark Mode Cnt Score Error Units
DevicesDetectBenchmark.deviceDetect thrpt 25 23.352 ± 2.876 ops/s
DevicesDetectBenchmark.yauaa thrpt 25 1.700 ± 0.125 ops/s

REMEMBER: The numbers above are just data. To gain reusable insights, you need to follow up on why the numbers are the way they are. Do not assume the numbers tell you what you want them to tell.

* - cache disabled