JS Questions (3466)
JS Questions (3466)
JS Questions (3466)
A closure involves a function and a surrounding state. A surrounding state is the lexical environment,
which means any variable in the same scope of the function to be returned. A closure is created then,
every time a function is called and its return value is another function that will hold a reference to all
of its surrounding state at the time of creation.
2. In a garbage-collected language like JavaScript, how are memory leaks possible? Describe
the low-level mechanics of a memory leak.
A memory leak is possible in a garbage collected language due to how the collection algorithms work
and how memory allocations take place in JavaScript.
Memory life cycle happens in 3 steps, 1st is memory allocation, 2nd is memory usage (read or write)
and 3rd is release of allocated memory. It’s the 3rd step that causes memory leaks, since in JavaScript
to determine if a block of allocated memory can be freed is an undecidable problem. For that reason
instead of verifying if an allocation of memory is “needed” we will check for references to a piece of
code, this can be done by counting references this may cause memory leaks due to unhandled
unused references or circular references when new allocations happen (moment where the GC runs)
and the other way of garbage collection would be to check for “reachability”, this involves checking
for root elements and allocated memory within this root elements. If at some point, a child element
of the root or parent is not accessible then the memory will be collected, however, memory leaks are
still possible in the case a reference to a parent node in HTML is deleted but a reference to one of its
children is still active in the JS, this will cause the memory allocation of the parent to remain active.
Which causes another memory leak.
3. What would be the challenges in implementing a dead code elimination tool that targets
JavaScript, as opposed to one that targets a language like C# or Java?
One of the main challenges of implementing a dead code elimination is the unused exports, side
effects and vendor dependencies. Since JavaScript is not a compiled language, only at runtime the
app can determine what code is used or not.
4. JavaScript performance has improved significantly over the past 10, 5 years, and is
continuing to do so. What are some of the implementation techniques that have enabled this?
Some big changes in JavaScript include native array functions, block scoped keywords (let and const)
the nullish coalescing operator (??) and the optional chaining operator (?.), arrow functions ()=>{},
async/await keywords for asynchronous development, setters and getters, inheritance and methods
from Classes implementation and module import/export.
5. New JavaScript language features have been added over the past 10, 5 years, too. Is it
possible that new language features can actually improve the performance of applications?
Or, would you expect new language features, in general, to have either zero or negative
effect on performance? Why or why not?
Yes, new language features can improve the performance of applications. An example of that is web
applications have taken advantage of each new feature released by the JavaScript team, and this is a
trend that will continue to exist. However this could not be the case every time since something
called dependencies may cause incompatibilities with newer versions of a language or framework,
but speaking performance-wise it is intuitive to believe that new features are meant to optimize and
improve the current status of any language