0

I was looking at the event loop in Javascript and it keeps mentioning the word heap. The only source I could find explaining was a small one-line definition on the MDN website:

Objects are allocated in a heap which is just a name to denote a large mostly unstructured region of memory.

Can someone provide a more detailed explanation of what a heap is in the sense of the JavaScript event loop, and its role in JavaScript?

the12
  • 2,395
  • 5
  • 19
  • 37
  • i assume you know what s stack and heap memory in any programming language. – RJ- Oct 11 '17 at 22:25
  • Unfortunately I do not. Did not have the pleasure of going through a CS program. – the12 Oct 11 '17 at 22:27
  • Related: https://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap (short, rough definition: heap is the memory where your objects are stored) – tevemadar Oct 11 '17 at 22:28
  • if you wanna know about stack and heap then refer link https://hashnode.com/post/does-javascript-use-stack-or-heap-for-memory-allocation-or-both-cj5jl90xl01nh1twuv8ug0bjk. you can also find plenty of resources out there. One main thing to make a note is memory consumed from teh javascript will be part of browser running the javascript. – RJ- Oct 11 '17 at 22:29
  • @tevemadar ok so like variables and objects you create within your program (strings, arrays, etc)? – the12 Oct 11 '17 at 22:40
  • Compound data, yes. Strings, array, objects in general. It is a place for storing 'bulky' data. But, saying 'variables' in general, would not be precise. – tevemadar Oct 11 '17 at 22:58
  • 1
    Possible duplicate of [What and where are the stack and heap?](https://stackoverflow.com/q/79923/218196) (or at least related) – Felix Kling Oct 11 '17 at 23:19
  • 1
    Notice it's called *the* heap, not *a* [heap](https://en.wikipedia.org/wiki/Heap_(data_structure)) (which is a different thing). And actually this doesn't have anything to do with the event loop in particular. – Bergi Oct 12 '17 at 02:21

1 Answers1

-1

A heap, in general (non JS) terms is well explained other-where on SO and also linked in the comments of the question.

The different JS implementations (engines) have stacks and heaps where they allocate memory, but that is not really exposed to you as a high level language user. JS is a garbage collected language, so it is often more helpful to think in terms of lifetime (scope) of the objects you create (with new or []) to avoid frequent garbage collection and recreation of objects.

Of possible interest: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_Management

and: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Variable_scope

visibleman
  • 3,175
  • 1
  • 14
  • 27