javascript - Are multiple objects being created? -


i'm working on simple react library i'm unsure whether multiple objects being created unnecessarily.

i have app.js file:

class app {   method1() {    }   method2() {    } } export default new app(); 

i have index.js file:

import app './app.js'; ... export default app; 

in index.js of react project (where make use of library) use:

import mylibrary 'react-library'; ... mylibrary.method1(); 

and same in of components too:

import mylibrary 'react-library'; ... mylibrary.method2(); 

is second import of mylibrary different object first mylibrary?

is second import of mylibrary different object first mylibrary?

in general object returned import cached (same behaviour nodejs require), multiples import of same file result in same object being returned. answer question no, you're dealing same reference in memory.

https://webpack.github.io/docs/resolving.html

every filesystem access cached multiple parallel or serial requests same resource merged.

in particular case, suggested in comments section, you're exporting instance, not class itself

export default new app(); 

consequently each component import file deal same instance. singleton pattern, don't know if desired behaviour, if want each component has it's own instance should export class instead.

export default app(); 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -