3rd-Eden / useragent

Useragent parser for Node.js, ported from browserscope.org
MIT License
897 stars 137 forks source link

Performance drop since 2.1.7 #78

Open yamsellem opened 9 years ago

yamsellem commented 9 years ago

We've found a performance drop in our production code when migrating to release 2.1.7. Here is a benchmark that reproduces it:

var userAgentParser = require('useragent');
var ua_chrome_10 = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/10.0.1664.3 Safari/537.36';

var Benchmark = require('benchmark');
var suite = new Benchmark.Suite();
suite.add('ua', function() {
    userAgentParser.parse(ua_chrome_10);
})
.on('cycle', function(event) {
    console.log(String(event.target));
})
.run({ 'async': true });

On node 0.10.24 (tested on 0.10.39 with similar results)

// useragent@2.1.6
ua x 112,676 ops/sec ±2.19% (88 runs sampled)

// useragent@2.1.7
ua x 33,641 ops/sec ±1.62% (89 runs sampled)

On node 0.12.6 (tested on 0.12.0 with similar results)

// useragent@2.1.6
ua x 109,132 ops/sec ±2.68% (85 runs sampled)

// useragent@2.1.7
ua x 20,552 ops/sec ±2.89% (87 runs sampled)

Only one commit differs between the two releases, commit #dff0db related to issue #76.

3rd-Eden commented 9 years ago

That's.. Interesting. Does it happend for all user-agent strings? Or just particular one?

yamsellem commented 9 years ago

We've noticed a 4% cpu overhead (on a 525req/sec server), so it may concerned a majority of strings.

3rd-Eden commented 9 years ago

It's odd as the only addition is new regular expressions and a re-order of the useragents.

STRML commented 7 years ago

Also seeing this drop in the suite:

~/g/f/u/benchmark (master|✚2…) $ node --version
v6.7.0
~/g/f/u/benchmark (master|✚2…) $ cat ../package.json | head -n 3
{
  "name": "useragent",
  "version": "2.1.9",
~/g/f/u/benchmark (master|✚2…) $ node run.js
Starting the benchmark, parsing 62 useragent strings per run

Executed benchmark against node module: "useragent"
Count (16), Cycles (4), Elapsed (5.516), Hz (273.25143832077316)

Executed benchmark against node module: "useragent_parser"
Count (32), Cycles (2), Elapsed (5.378), Hz (531.7064666225433)

Executed benchmark against node module: "useragent-parser"
Count (17), Cycles (5), Elapsed (5.587), Hz (253.35079923369597)

Executed benchmark against node module: "ua-parser"
Count (67), Cycles (3), Elapsed (5.371), Hz (1110.320560518609)

Module: "ua-parser" is the user agent fastest parser.