Found 6710 Articles for Javascript

How to freeze an Object in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

177 Views

In the Real time world javascript doesn't have traditional classes as seen in other languages. It has objects and constructors. Object.freeze() is one among many constructor methods helps to freeze an object.Freezing an object does not allow new properties to be added to the object and also prevents the object from changing its own properties. Object.freeze() will always try to preserve the enumerability, configurability, writability and the prototype of the object. It won't create a frozen copy.Applications1) freeze() is used for freezing objects and arrays.2) freeze() is used to make an object immutable.SyntaxObject.freeze(obj)ExampleLive Demo // an object is created ... Read More

What are the differences between mean.io and mean.js in javascript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

141 Views

Differences between Mean.io and Mean.jsThe MEAN is a stack framework. When combined with Mongodb, node.js, express.js and angular.js it helps to create a complete javascript web app. A software developer from Israel, Amos Haviv was the first person to initiate Mean.io. Mean.js is simply a fork out from Mean.io. When developers closely observe these two variations they perceive that Mean.io has a different objective than Mean.js. The only reason might be that Mean.io is not as elegant as Mean.js. When a developer completely understands Stack, mostly he prefers Mean.js.Let's look where Mean.io and Mean.js differ1) Boilerplate generation and scaffoldingThese are ... Read More

What is the difference between Map and WeakMap in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

4K+ Views

Differences between Map and WeakMapThe functional mechanism of Map and WeakMap is same but they have little differences.1) A WeakMap accepts only objects as keys whereas a Map, in addition to objects, accepts primitive datatype such as strings, numbers etc.2) WeakMap objects doesn't avert garbage collection if there are no references to the object which is acting like a key. Therefore there is no method to retrieve keys in WeakMap, whereas in Map there are methods such as Map.prototype.keys() to get the keys.3) There is no size property exists in WeakMap.MapIt is used to associate a key to a value ... Read More

How can closures cause memory leak and how to prevent it?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

2K+ Views

ClosureOne of the strengths of javascript is closures. Javascript allows nested functions, a function inside a function, to access the variables of parent functions. This process of accessing outer function's variable by an inner function is called a closure. A memory leak occurs when a declared variable is automatically available to the inner nested function and resides in a memory despite it is not referenced in the inner nested function.In the following example 'childFunction' is the inner function defined within the 'parentFunction' the outer function. When a call is made to 'parentFunction' with a parameter "outer val", the outer variable ... Read More

How to implement quick sort in JavaScript?

Nikhilesh Aleti
Updated on 19-Dec-2022 16:54:07

3K+ Views

In this article, we are going to discuss how to implement quick sort in JavaScript with suitable examples. Quick sort The Quick sort is a divide and conquers algorithm similar to the merge sort. In this, we pick a pivot element and divide the array around the pivot element. There are many ways to pick the pivot element. Always pick the first element as a pivot element. Always pick the last element as a pivot element. (Implemented in the below program) Pick a random element as pivot element Pick the middle element as pivot element. The main process ... Read More

How can Forgotten timers or callbacks cause memory leaks in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

702 Views

Forgotten timers/callbacksThere are two timing events in javascript namely setTimeout() and setInterval(). The former executes a function after waiting a specified number of milliseconds, whereas the latter executes a function periodically(repeats for every certain interval of time).When any object is tied to a timer callback, it will not be released until the timeout happens. In this scenario timer resets itself and runs forever till the completion of timeout there by disallowing garbage collector to remove the memory.These timers are most frequent cause of memory leaks in javascript.ExampleIn the following example, the timer callback and its tied object(tiedObject) will not be ... Read More

How can Detached DOM elements cause memory leak in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

2K+ Views

Detached Dom elementsDetached DOM elements are the elements which have been removed from the DOM but their memory is still retained because of JavaScript. This means that as long the element have a reference to any variable or an object anywhere, it does not garbage collected even after destroyed from the DOM.DOM is like an double-linked tree which means a reference to a node in the tree will halt the entire tree from garbage collection.Let's take an example of creating a DOM element in javascript. After creating the element destroy it but forget to delete the variable holding it. This ... Read More

What is the use of Map in JavaScript?

vineeth.mariserla
Updated on 30-Jul-2019 22:30:26

241 Views

MapMap holds key value pairs and remembers the actual insertion order of the keys. Map allows to store only a unique value.syntaxnew Map([iterable])Case-1: Absence Of MapIn the absence of Map, since javascript object endorses only one key object, If we provide multiple keys only the last one will be remembered. In the following example despite providing many keys such as a and b only b is remembered and displayed as output.So to eliminate this drawback "Map" came in to existence in javascript.ExampleLive Demo    const x = {};    const a = {};    const b = { ... Read More

How to clone an object in JavaScript?

Nikhilesh Aleti
Updated on 18-Nov-2022 07:19:09

5K+ Views

An object is said to be an entity which has properties and types in it. Example, consider a Person as an object and it has properties like height, weight, age and salary. In the similar way JavaScript also have objects and their defined properties. An object in JavaScript is a complicated data type where it can store various data types in it. Let’s consider this example below const employee = { name : ‘Dhoni’, age : 41, height : 5.8, } Cloning an object using JavaScript In general, “Cloning” is ... Read More

Explain in detail about Mark and Sweep algorithm in JavaScript?

vineeth.mariserla
Updated on 04-Jul-2020 15:01:53

1K+ Views

Mark and Sweep algorithmMark and Sweep algorithm looks out for objects 'which are unreachable' rather than objects 'which are no longer needed'. This algorithm is the improvement of Reference-counting algorithm.This algorithm actually goes through 3 important steps. Root: In general, a root is a global variable that is used in the code. A window object in javascript can act as a   root. This algorithm uses global object root to find whether the objects are reachable or unreachable.This algorithm then monitors every root and also their children. While monitoring, some objects which are reachable are marked and remaining objects which are ... Read More

Advertisements