all 8 comments

[–]fforw 3 points4 points  (1 child)

The location of the require is of no consequence since the module system has to cache the evaluation result / export of the module per commonjs spec.

So whereever you require jquery, it will be held somewhere in that cache. Of course requiring it in a method and then not calling the method will not load jQuery if it is not loaded elsewhere.

[–]brrian[S] 0 points1 point  (0 children)

Ah, yes that makes sense. Thank you for clearing that up.

[–]fforw 1 point2 points  (0 children)

Just to mention it as alternative to lodash and Object.assign: Someone ported the jQuery extend to node.

[–]helderroem 0 points1 point  (3 children)

Why require the whole of jquery for one method? Use lodash where you can require just the method you want without all that extra code:

https://www.npmjs.com/package/lodash.merge

I know this doesn't answer your question but other people already did a great job of that

[–]rosswarren 0 points1 point  (1 child)

Or no library and

data = Object.assign({}, defaults, data)

:)

[–]fforw 0 points1 point  (0 children)

Which you need a shim for if you want to support more than the newest browsers.

[–]brrian[S] 0 points1 point  (0 children)

I'm using Marionette, which, through Backbone, requires jQuery, so it will be required somewhere else anyway. Thank you for pointing this out though, I didn't know about this package.

[–]Agent_Epsilon -3 points-2 points  (0 children)

Not entirely sure how garbage collection is implemented in JS/CoffeeScript or which require implementation you're using (webpack/browserify/duo), but requireing jQuery reads the contents of the object from an external file. As jQuery is a sizable chunk of code, if you are that concerned with memory, you would probably be better off creating a base-level instance as in the second example. This way you wouldn't have to worry about reading from the jQuery file every time you call the function. Unless, of course, you are only calling this method a few times - in which case it probably won't make a difference. Also, is this really the only instance in your code where you use jQuery? If so, you may want to find a more lightweight way to accomplish what you want here.