Thursday, 16 February 2017

Improving Angular 2 Performance while using ngFor

When you iterate over an array or collection most probably you use ngFor directive to bind the data of collection into your DOM elements.

Problem !

Here the big problem is, If at some point of time you wish to change the data in the collection, for example maybe as a result of an API request, Then the Angular can’t keep track of items in the collection and has no knowledge of which items have been removed or added as a results it creates the DOM again. That means this has a lot of DOM manipulations.

Solution ..

Here comes the solution, ngFor directive has a property called track by.

So what do these do? They help Angular to associate key with the DOM elements and tracks which items are added or removed.

Angular’s ngFor defaults tracks object identity for you, which is fast, but it can be more faster if we use track by unique object ID in ngFor. Now when you change the collection, Angular can track which items have been added or removed according to the unique identifier and create or destroy only the things that changed.

   <li *ngFor="let student of students; trackBy: student.id;">
    ...
   </li>

Optionally, we can use track by index but that is not advisable as the index may be subject to change if you reorder your collection. Using a unique identifier allows Angular to locate that DOM node associated with the object much faster

Toggle String Lower to Upper and Upper to Lower

You have been given a String S consisting of uppercase and lowercase English alphabets. You need to change the case of each alphabet in this String. That is, all the uppercase letters should be converted to lowercase and all the lowercase letters should be converted to uppercase. You need to then print the resultant String to output.

using System; 
using System.Numerics;
public static class InvertStringExtension
{
    public static string Invert(this string s)
    {
        char[] chars = s.ToCharArray();
        for (int i = 0; i < chars.Length; i++)
            chars[i] = chars[i].Invert();
            
        return new string(chars);
    }
    public static char Invert(this char c)
    {
        if (!char.IsLetter(c))
            return c;
 
        return char.IsUpper(c) ? char.ToLower(c) : char.ToUpper(c);
    }
}
class MyClass {
    static void Main(string[] args) {
        var line1 = System.Console.ReadLine().Trim();
        System.Console.WriteLine(line1.Invert());
    }
}

Tuesday, 14 February 2017

Maintaining Relationship in NoSQL

The decision "What database to use" is a bit difficult. Most Startup rely on NoSQL Database. MongoDB is the most popular NoSQL DataBase. NoSQL Database has grown in popularity due to it's open source, flexible in nature and ease to use.

But what about relationship

As Database is mostly representation of real world entities relationship is very important to be maintain among database. While MongoDB is not a relational database, there are two recommended approaches to representing relationships between entities.

Modeling Embedded Relationships

In this approach we embed the related document inside its parent document.

{
   "_id":ObjectId("52ffc33cd85242f436000001"),
   ...
   "address": [
      {
         "building": "22 A, Indiana Apt",
         ...
      },
      {
         "building": "ABC Apart",
         ...
      }
   ]
} 

This approach maintains all the related data in a single document, which makes it easy to retrieve and maintain. But The drawback is it is not Consistent. And if the embedded document keeps on growing too much in size, it can impact the read/write performance.

Modeling Referenced Relationships

In this approach the two related table are maintained saperatly and the reference of one table is maintained in another table by its document id

{
   ...
   "address_ids": [
      ObjectId("52ffc4a5d85242602e000000"),
      ObjectId("52ffc4a5d85242602e000001")
   ]
}

The benefit of this approach is whenever a document is updated, there is no overhead of managing other document because the reference ids does not change and the problem of Consistency is solved.