ua-parser-js icon indicating copy to clipboard operation
ua-parser-js copied to clipboard

Node version should return singleton.

Open Redsandro opened this issue 9 years ago • 2 comments

Every module that uses the parser needs to instantiate their own parser:

var UAParser = require('ua-parser-js');
var parser = new UAParser();
// do stuff with parser...

This is wasting some memory, because each module could share the same instance if ua-parser-js would return a singleton version of the parser, so that you can just do this:

var parser = require('ua-parser-js');
// do stuff with parser...

Redsandro avatar Jun 10 '15 13:06 Redsandro

Hi,

I think that is a very valid point. On the other hand I never (myself) initiate the parser more than once on our system. @Redsandro Why do you need to initiate it multiple times? And in the same environment?

KR, ebbmo

ebbmo avatar Jun 20 '17 22:06 ebbmo

@ebbmo on different routes in different files. Technically the new ... part could be in a separate custom module, but that would be working around a quirk. It's better to resolve the quirk.

Some modules have this construct. You can recognize them from 20 requires and 2 new ...s.

If nothing else, syntactic sugar.

(Slightly late response, not sure if this is still relevant.)

Redsandro avatar Nov 05 '18 12:11 Redsandro