Skip to content
Browse files

[fix] use LRU for caching, so we don't create a memory "leak". Fixes #22

  • Loading branch information...
1 parent e3611a5 commit f37e46ca82cb1c5264684218fda1b3133777fd4e @3rd-Eden committed
Showing with 56 additions and 47 deletions.
  1. +7 −3 index.js
  2. +49 −44 package.json
View
10 index.js
@@ -478,11 +478,15 @@ exports.parse = function parse(userAgent, jsAgent) {
* @param {String} jsAgent Optional UA from js to detect chrome frame
* @api public
*/
-var dictionary = {};
+var LRU = require('lru-cache')(5000);
exports.lookup = function lookup(userAgent, jsAgent) {
- var key = (userAgent || '')+(jsAgent || '');
+ var key = (userAgent || '')+(jsAgent || '')
+ , cached = LRU.get(key);
- return dictionary[key] || (dictionary[key] = exports.parse(userAgent, jsAgent));
+ if (cached) return cached;
+ LRU.set(key, (cached = exports.parse(userAgent, jsAgent)));
+
+ return cached;
};
/**
View
93 package.json
@@ -1,47 +1,52 @@
{
- "name": "useragent"
- , "version": "2.0.1"
- , "description": "Fastest, most accurate & effecient user agent string parser, uses browserscope's research for parsing"
- , "author": "Arnout Kazemier"
- , "main": "./index.js"
- , "keywords":[
- "agent"
- , "browser"
- , "browserscope"
- , "os"
- , "parse"
- , "parser"
- , "ua"
- , "ua-parse"
- , "ua-parser"
- , "user agent"
- , "user"
- , "user-agent"
- , "useragent"
- , "version"
- ]
- , "maintainers": [{
- "name":"Arnout Kazemier"
- , "email":"info@3rd-Eden.com"
- , "web":"http://www.3rd-Eden.com"
- }]
- , "license": {
- "type": "MIT"
- , "url": "https://github.com/3rd-Eden/useragent/blob/master/LICENSE"
- }
- , "repository": {
- "type": "git"
- , "url" : "http://github.com/3rd-Eden/useragent.git"
- }
- , "devDependencies": {
- "should": "*"
- , "mocha": "*"
- , "long-stack-traces": "0.1.2"
- , "yamlparser": "0.0.2"
- , "request": "2.9.203"
- , "semver": "1.0.14"
- }
- , "scripts": {
- "test": "make test"
+ "name": "useragent",
+ "version": "2.0.1",
+ "description": "Fastest, most accurate & effecient user agent string parser, uses browserscope's research for parsing",
+ "author": "Arnout Kazemier",
+ "main": "./index.js",
+ "keywords": [
+ "agent",
+ "browser",
+ "browserscope",
+ "os",
+ "parse",
+ "parser",
+ "ua",
+ "ua-parse",
+ "ua-parser",
+ "user agent",
+ "user",
+ "user-agent",
+ "useragent",
+ "version"
+ ],
+ "maintainers": [
+ {
+ "name": "Arnout Kazemier",
+ "email": "info@3rd-Eden.com",
+ "web": "http://www.3rd-Eden.com"
}
+ ],
+ "license": {
+ "type": "MIT",
+ "url": "https://github.com/3rd-Eden/useragent/blob/master/LICENSE"
+ },
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/3rd-Eden/useragent.git"
+ },
+ "devDependencies": {
+ "should": "*",
+ "mocha": "*",
+ "long-stack-traces": "0.1.2",
+ "yamlparser": "0.0.2",
+ "request": "2.9.203",
+ "semver": "1.0.14"
+ },
+ "scripts": {
+ "test": "make test"
+ },
+ "dependencies": {
+ "lru-cache": "~2.2.2"
+ }
}

0 comments on commit f37e46c

Please sign in to comment.
Something went wrong with that request. Please try again.