3

Are there rules or best practices to write JavaScript code that is not memory hungry?

Back in time when I had to allocate and deallocate each byte in my software, I had a good picture of memory usage in my mind. But now I am alsways uncertain. All those cross and circular references, closures, duck typing, I got used to those great features but always feel uncertain about the memory effectivnes.

exebook
  • 32,014
  • 33
  • 141
  • 226
  • 1
    Have you taken a look at this before? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management – admdrew Sep 23 '14 at 20:59
  • my answer is not use js ;) really -- but also I agree with Dai – Daij-Djan Sep 23 '14 at 21:01
  • Well, knowing how your car works and how it is designed is one thing, but having a good set of practical rules how not to kill it too fast is another thing. – exebook Sep 23 '14 at 21:04
  • 1
    Are you referring to client-side JS (browser related) or server side (node.js)? While some fundamentals might be the same, I believe the actual answer will be quite different depending on this. – fstanis Sep 23 '14 at 21:04
  • possible duplicate of [Javascript memory management pitfalls?](http://stackoverflow.com/questions/1519114/javascript-memory-management-pitfalls) – fstanis Sep 23 '14 at 21:52
  • @Dai **[Please stop using Programmers.SE as your toilet bowl](http://meta.stackexchange.com/q/73382/165773)**. See http://meta.programmers.stackexchange.com/questions/6483/why-was-my-question-closed-or-down-voted/6490#6490 – gnat Sep 27 '14 at 09:31
  • I will ignore this but as an advice: dont be insulting when you want something. – Daij-Djan Sep 27 '14 at 13:12

1 Answers1

0

Anytime you exit a scope, the garbage collector does its job. So if you move some code to a function that executes and ends, all of the variables will be garbage collected when the function is over (except for the ones that were available before the function was called in the first place).

Nick Manning
  • 2,828
  • 1
  • 29
  • 50
  • 1
    What about objects in function scope that are also closed over? The statement "executes and ends, all of the variables will be garbage collected when the function is over" is incorrect. – Jamie Dixon Sep 23 '14 at 21:11
  • Local objects are garbage collected when the function scope is over, no? – Nick Manning Sep 23 '14 at 21:12
  • No. That's just not how it works. The garbage collection algorithms used by browsers take varying routes, but none of them go for "function ended, garbage collect". Closures are a fundamental part of Javascript and GCing function scoped variables at function end time would decimate this concept. – Jamie Dixon Sep 23 '14 at 21:16
  • Isn't a closure and the inner part of a function basically the same thing? – Nick Manning Sep 23 '14 at 21:18
  • I don't think that's 100% correct - objects are garbage collected when their reference count hits zero, regardless of the scope they were defined in. This has the consequence of garbage collecting all local objects, *assuming they were not also referenced from another scope*. In other words, they are not garbage collected because they are local, but because the local reference is the only reference that exists at that point. – fstanis Sep 23 '14 at 21:19
  • 1
    All modern browsers use the Mark-and-sweep algorithm for GC. It's not enough to do reference counting. You can read more about this here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management – Jamie Dixon Sep 23 '14 at 21:20
  • @NickManning They're not the same thing. A function can end and set up other code to execute some other time. That latter function still has access to variables defined in the first function, even though it's "ended". This is, at a very basic level, what a closure is. – Jamie Dixon Sep 23 '14 at 21:22