Permalink
Browse files

[minor] Performance improvements

  • Loading branch information...
1 parent 6a26417 commit caf9e60cb02e5d043ee81b696044960b33c0c89b @3rd-Eden committed Jan 9, 2013
Showing with 594 additions and 333 deletions.
  1. +31 −15 benchmark/dict.js
  2. +1 −1 benchmark/package.json
  3. +62 −0 benchmark/regression.js
  4. +1 −1 benchmark/run.js
  5. +7 −1 bin/update.js
  6. +42 −31 index.js
  7. +277 −277 lib/regexps.js
  8. +158 −0 lib/update-slow.js
  9. +15 −7 lib/update.js
View
@@ -1,7 +1,8 @@
+'use strict';
+
/**
* Benchmark dependencies
*/
-
var microtime = require('microtime')
, benchmark = require('benchmark')
, yaml = require('yamlparser')
@@ -11,18 +12,20 @@ var microtime = require('microtime')
/**
* Useragent parsers
*/
-
var useragent2 = require('../')
, useragent = require('useragent')
- , uaparser = require('ua-parser');
+ , uaparser = require('ua-parser')
+ , useragent_parser = require('useragent_parser')
+ , useragent_parser2 = require('useragent-parser');
/**
- * Setup the test-files
+ * Setup the test-files.
*/
-
var useragentlist = path.join(__dirname, '..', 'tests', 'fixtures', 'testcases.yaml')
, yammy = yaml.eval(fs.readFileSync(useragentlist).toString()).test_cases
- , testcases = yammy.map(function (test) { return test.user_agent_string })
+ , testcases = yammy.map(function (test) {
+ return test.user_agent_string;
+ })
, length = testcases.length;
/**
@@ -31,12 +34,13 @@ var useragentlist = path.join(__dirname, '..', 'tests', 'fixtures', 'testcases.y
var froomfroom = new benchmark.Suite;
-froomfroom.add('useragent2', function () {
+froomfroom
+.add('useragent latest', function () {
for (var i = 0; i < length; i++ ) {
useragent2.parse(testcases[i]);
}
})
-froomfroom.add('useragent2 dictionary', function () {
+.add('useragent latest dictionary', function () {
for (var i = 0; i < length; i++ ) {
useragent2.lookup(testcases[i]);
}
@@ -46,22 +50,34 @@ froomfroom.add('useragent2 dictionary', function () {
useragent.parser(testcases[i]);
}
})
+.add('useragent_parser', function () {
+ for (var i = 0; i < length; i++ ) {
+ useragent_parser.parse(testcases[i]);
+ }
+})
+.add('useragent-parser', function (){
+ for (var i = 0; i < length; i++ ) {
+ useragent_parser2.parse(testcases[i]);
+ }
+})
.add('ua-parser', function () {
for (var i = 0; i < length; i++ ) {
uaparser.parse(testcases[i]);
}
})
-.on('cycle', function (bench, details) {
+.on('cycle', function (event) {
+ var details = event.target;
+
console.log('Executed benchmark (%s)', details.name);
console.log('Count (%d), Cycles (%d), Elapsed (%d), Hz (%d)\n'
- , details.count
- , details.cycles
- , details.times.elapsed
- , details.hz
- );
+ , details.count
+ , details.cycles
+ , details.times.elapsed
+ , details.hz
+ );
})
.on('complete', function () {
- console.log(this.filter('fastest').pluck('name') + ' has/have the fastest parser');
+ console.log(this.filter('fastest').pluck('name') + ' is the fastest parser');
});
/**
View
@@ -5,7 +5,7 @@
, "dependencies": {
"microtime": ""
, "benchmark": ""
- , "useragent": "0.1.2"
+ , "useragent": "1.1.0"
, "ua-parser": "0.3.1"
, "useragent_parser": "1.0.0"
, "useragent-parser": "0.1.1"
View
@@ -0,0 +1,62 @@
+'use strict';
+
+/**
+ * Benchmark dependencies.
+ */
+var microtime = require('microtime')
+ , benchmark = require('benchmark')
+ , yaml = require('yamlparser')
+ , path = require('path')
+ , fs = require('fs');
+
+/**
+ * Useragent parsers.
+ */
+var useragent2 = require('../')
+ , useragent = require('useragent');
+
+/**
+ * Setup the test-files.
+ */
+var useragentlist = path.join(__dirname, '..', 'tests', 'fixtures', 'testcases.yaml')
+ , yammy = yaml.eval(fs.readFileSync(useragentlist).toString()).test_cases
+ , testcases = yammy.map(function (test) {
+ return test.user_agent_string;
+ })
+ , length = testcases.length;
+
+/**
+ * Setup the benchmark
+ */
+var froomfroom = new benchmark.Suite;
+
+froomfroom
+.add('useragent latest', function () {
+ for (var i = 0; i < length; i++ ) {
+ useragent2.parse(testcases[i]);
+ }
+})
+.add('useragent1', function () {
+ for (var i = 0; i < length; i++ ) {
+ useragent.parse(testcases[i]);
+ }
+})
+.on('cycle', function (event) {
+ var details = event.target;
+
+ console.log('Executed benchmark (%s)', details.name);
+ console.log('Count (%d), Cycles (%d), Elapsed (%d), Hz (%d)\n'
+ , details.count
+ , details.cycles
+ , details.times.elapsed
+ , details.hz
+ );
+})
+.on('complete', function () {
+ console.log(this.filter('fastest').pluck('name') + ' is the fastest parser');
+});
+
+/**
+ * Start the benchmark, froom frooom!!
+ */
+froomfroom.run();
View
@@ -71,7 +71,7 @@ froomfroom
);
})
.on('complete', function () {
- console.log(this.filter('fastest').pluck('name') + ' has/have the fastest parser');
+ console.log(this.filter('fastest').pluck('name') + ' is the fastest parser');
});
/**
View
@@ -6,6 +6,12 @@
* Update our definition file.
*/
require('../lib/update').update(function updating(err, data) {
- if (err) return console.error('Update unsuccessfull due to reasons', err.message);
+ if (err) {
+ console.error('Update unsuccessfull due to reasons');
+ console.log(err.message);
+ console.log(err.stack);
+
+ return;
+ }
console.log('Successfully fetched and generated new parsers from the internets.');
});
View
@@ -55,23 +55,23 @@ Object.defineProperty(Agent.prototype, 'os', {
, res;
for (; i < length; i++) {
- if (res = userAgent.match(parsers[i].parser)) {
+ if (res = userAgent.match(parsers[i][0])) {
parser = parsers[i];
- if (parser.family) res[1] = parser.family.replace('$1', res[1]);
- if (parser.major) res[2] = parser.major;
- if (parser.minor) res[3] = parser.minor;
- if (parser.patch) res[4] = parser.patch;
-
+ if (parser[1]) res[1] = parser[1].replace('$1', res[1]);
break;
}
}
- // Default to Array if we didn't find a match.
- res = res || [];
-
return Object.defineProperty(this, 'os', {
- value: new OperatingSystem(res[1], res[2], res[3], res[4])
+ value: !parser || !res
+ ? new OperatingSystem()
+ : new OperatingSystem(
+ res[1]
+ , parser[2] || res[2]
+ , parser[3] || res[3]
+ , parser[4] || res[4]
+ )
}).os;
},
@@ -106,23 +106,23 @@ Object.defineProperty(Agent.prototype, 'device', {
, res;
for (; i < length; i++) {
- if (res = userAgent.match(parsers[i].parser)) {
+ if (res = userAgent.match(parsers[i][0])) {
parser = parsers[i];
- if (parser.family) res[1] = parser.family.replace('$1', res[1]);
- if (parser.major) res[2] = parser.major;
- if (parser.minor) res[3] = parser.minor;
- if (parser.patch) res[4] = parser.patch;
-
+ if (parser[1]) res[1] = parser[1].replace('$1', res[1]);
break;
}
}
- // Default to Array if we didn't find a match.
- res = res || [];
-
return Object.defineProperty(this, 'device', {
- value: new Device(res[1], res[2], res[3], res[4])
+ value: !parser || !res
+ ? new Device()
+ : new Device(
+ res[1]
+ , parser[2] || res[2]
+ , parser[3] || res[3]
+ , parser[4] || res[4]
+ )
}).device;
},
@@ -374,20 +374,25 @@ exports.parse = function parse(userAgent, jsAgent) {
, res;
for (; i < length; i++) {
- if (res = userAgent.match(parsers[i].parser)) {
+ if (res = parsers[i][0].exec(userAgent)) {
parser = parsers[i];
- if (parser.family) res[1] = parser.family.replace('$1', res[1]);
- if (parser.major) res[2] = parser.major;
- if (parser.minor) res[3] = parser.minor;
- if (parser.patch) res[4] = parser.patch;
+ if (parser[1]) res[1] = parser[1].replace('$1', res[1]);
+ if (!jsAgent) return new Agent(
+ res[1]
+ , parser[2] || res[2]
+ , parser[3] || res[3]
+ , parser[4] || res[4]
+ , userAgent
+ );
break;
}
}
- // Default to Array if we didn't find a match.
- res = res || [];
+ // Return early if we didn't find an match, but might still be able to parse
+ // the os and device, so make sure we supply it with the source
+ if (!parser || !res) return new Agent('', '', '', '', userAgent);
// Detect Chrome Frame, but make sure it's enabled! So we need to check for
// the Chrome/ so we know that it's actually using Chrome under the hood.
@@ -397,12 +402,18 @@ exports.parse = function parse(userAgent, jsAgent) {
// Run the JavaScripted userAgent string through the parser again so we can
// update the version numbers;
parser = parse(jsAgent);
- res[2] = parser.major;
- res[3] = parser.minor;
- res[4] = parser.patch;
+ parser[2] = parser.major;
+ parser[3] = parser.minor;
+ parser[4] = parser.patch;
}
- return new Agent(res[1], res[2], res[3], res[4], userAgent);
+ return new Agent(
+ res[1]
+ , parser[2] || res[2]
+ , parser[3] || res[3]
+ , parser[4] || res[4]
+ , userAgent
+ );
};
/**
Oops, something went wrong.

0 comments on commit caf9e60

Please sign in to comment.