I have a question related to modules and how they work in terms of memory and duplicate?Let's say I have my app.js file that opens a variable.``` var modelIndex = require('models/index.js'); var books = require('data/books.js'); var app = require('express'); app.get('/index', modelIndex);```How does nodejs handle books if I open it in another module? In the example above the modelIndex.``` var books = require('data/books.js'); module.exports = (req, res) => { books.dictionary.date = '2019-10-01'; res.send('updated'); }``` Are both of the book variables the same variable in memory? or do they act as pointers or 2 separate variables all together? After testing this out, it seems I can change the value even though a required file and the changes appear on all other versions which makes me think it just acts as a pointer, but when testing the memory usage it goes way beyond the usage of just a normal pointer. If I instead send the book variable with the req, res the memory is greatly reduced. Just wondering your thoughts on this and how it works under the hood. This could potentially be useful in optimizing larger projects, so any help would be useful.
Submitted October 04, 2019 at 08:46PM by bryku
No comments:
Post a Comment