Script Languages Archives - https://www.theoryofcomputation.co/category/script-languages/ Science of Computer Mon, 13 Jul 2020 14:45:14 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 https://i0.wp.com/www.theoryofcomputation.co/wp-content/uploads/2018/08/cropped-favicon-512x512-2.png?fit=32%2C32&ssl=1 Script Languages Archives - https://www.theoryofcomputation.co/category/script-languages/ 32 32 149926143 How to Improve the Performance of Angular Application? https://www.theoryofcomputation.co/how-to-improve-the-performance-of-angular-application/ Mon, 13 Jul 2020 14:41:32 +0000 https://www.theoryofcomputation.co/?p=509 How to Improve the Performance of Angular Application? Angular is one of the most used frameworks for application development. It is especially preferred by developers that need to design a quality dynamic app. The framework follows a modular web development approach, which makes it quite a popular option for web app developers. But, do you...

The post How to Improve the Performance of Angular Application? appeared first on .

]]>
How to Improve the Performance of Angular Application?

Angular is one of the most used frameworks for application development. It is especially preferred by developers that need to design a quality dynamic app. The framework follows a modular web development approach, which makes it quite a popular option for web app developers. But, do you know you could improve the performance of your Angular app and make it even more efficient? Fortunately, yes! You can create a high-quality dynamic app with Angular and improve your app with some effective tricks.

Let’s check some easy and effective ways to boost the performance of your Angular app.

Minimize Change Detections

Angular Framework runs change detection in the component. This change detection will detect all the changes in your data. Despite being one of the fastest dynamic app development platforms, Angular has to work harder in order to monitor all the changes.

You could minimize the work of this Framework by giving it an indication regarding when to detect the changes in the component. By changing the default change detection feature to ChangeDetectionStrategy.OnPush, you can reduce the workload of the framework. The On-push strategy will tell Angular to detect changes when you run the detection manually or there is a change in Input reference.

By default, components in an Angular application undergo change detection with nearly every user interaction. But, Angular allows you take control of this process. You can indicate to Angular if a component subtree is up to date and exclude it from change detection.

The first way to exclude a component subtree from change detection is by setting the `changeDetection` property to `ChangeDetectionStrategy.OnPush` in the @Component decorator. This tells Angular that the component only needs to be checked if an input has changed, and that all of the inputs can be considered immutable.

At the price of a bit more complexity it is possible to increase the amount of control. For example, by injecting the ChangeDetectorRef service, you can inform Angular that a component should be detached from change detection. You can then manually take control of calling `reattach` or `detectChanges()` yourself, giving you full control of when and where a component subtree is checked.

Minimize DOM Manipulations

If you ever need to change the data in Angular (perhaps, because of the API request), you will face a lot of issues. This is because Angular does not monitor the data in your collection. It doesn’t know which items are added and deleted.

By adding the Trackby functionality, you could request Angular to detect the data that has been added and deleted based on the unique identifier. This way Angular will remove the items that changed.

By default, when iterating over a list of objects, Angular will use object identity to determine if items are added, removed, or rearranged. This works well for most situations. However, with the introduction of immutable practices, changes to the list’s content generates new objects.

In turn, ngFor will generate a new collection of DOM elements to be rendered. If the list is long or complex enough, this will increase the time it takes the browser to render updates. To mitigate this issue, it is possible to use trackBy to indicate how a change to an entry is determined.

Angular Observables Tutorial

The post How to Improve the Performance of Angular Application? appeared first on .

]]>
509
Regular Expression Using Perl https://www.theoryofcomputation.co/regular-expression-using-perl/ Mon, 03 Sep 2018 17:07:52 +0000 https://www.theoryofcomputation.in/?p=176 Regular Expression Using Perl : Perl is the language that is the most famous for its use of regular expression for good reasons. We use the =~ operator to denote a match or an assignment depending upon the context. The use of !~ is to reverse the sense of the match. There are basically two regex operators in perl: Matching: m//...

The post Regular Expression Using Perl appeared first on .

]]>
Regular Expression Using Perl : Perl is the language that is the most famous for its use of regular expression for good reasons.

We use the =~ operator to denote a match or an assignment depending upon the context. The use of !~ is to reverse the sense of the match.

There are basically two regex operators in perl:

  • Matching: m//
  • Substitution: s///

The purpose of the // is to enclose the regex. However, any other delimiters like {}</codmy ($hours, $minutes, $seconds) = ($time =~ m/(\d+):(\d+):(\d+)/); e>, "", etc could be used.

Matching

To use the matching operator, we simply check both sides using the =~ and m// operator.

The following sets $true to 1 if and only if $foo matches the regular expression foo:

$true = ($foo =~ m/foo/);

It is not difficult to see that just the opposite is achieved with !~:

$false = ($foo !~ m/foo/);

Capturing

As promised, the () could be used for capturing parts of the regexes. When the pattern inside a parentheses match, they go into special variables like $1$2, etc in that order.

Example:

Here’s how one would extract the hours, minutes, seconds from a time string:

if ($time =~ /(\d\d):(\d\d):(\d\d)/) { # match hh:mm:ss format
 $hours = $1;
 $minutes = $2;
 $seconds = $3;
}

In list context, the list ($1, $2, $3, .. ) would be returned.

my ($hours, $minutes, $seconds) = ($time =~ m/(\d+):(\d+):(\d+)/);

Substitution

This is our favorite search and replace feature. Almost the same syntax rules apply here except that there is an extra clause between the second // that tells us what to match with.

$x = "Time to feed the cat!";
$x =~ s/cat/hacker/; # $x contains "Time to feed the hacker!"
if ($x =~ s/^(Time.*hacker)!$/$1 now!/) {
 $more_insistent = 1;
}
$y = "'quoted words'";
$y =~ s/^'(.*)'$/$1/; # strip single quotes,
# $y contains "quoted words"

Modifiers

Modifiers could be appended to the end of the regex operation expression to modify their matching behavior.

Here is a list of some important modifiers:

Modifier Description
i Case insensisitive matching
s Allows the use of . to match newlines
x Allows use of whitespace in the regex for clarity
g Globally find all matches

Here’s how one might want to use the g modifier:

$x = "I batted 4 for 4";
$x =~ s/4/four/; # doesn't do it all:
# $x contains "I batted four for 4"

$x = "I batted 4 for 4";
$x =~ s/4/four/g; # does it all:
# $x contains "I batted four for four"

The post Regular Expression Using Perl appeared first on .

]]>
176
Angular Observables Tutorial https://www.theoryofcomputation.co/angular-observables-tutorial/ Sun, 02 Sep 2018 15:06:32 +0000 https://www.theoryofcomputation.in/?p=173 Angular Observables Angular Observables provide support for passing messages between publishers and subscribers in your application. Observables offer significant benefits over other techniques for event handling, asynchronous programming, and handling multiple values. Observables are declarative—that is, you define a function for publishing values, but it is not executed until a consumer subscribes to it. The...

The post Angular Observables Tutorial appeared first on .

]]>
Angular Observables

Angular Observables provide support for passing messages between publishers and subscribers in your application.

Observables offer significant benefits over other techniques for event handling, asynchronous programming, and handling multiple values.

Observables are declarative—that is, you define a function for publishing values, but it is not executed until a consumer subscribes to it. The subscribed consumer then receives notifications until the function completes, or until they unsubscribe.

In a real-world example, we can say that the Internet service offered by mobile devices is an observable. It is available only to the people who have subscribed to it. We continuously receive this service from the service provider only as long as this service is on and we are subscribed to it.

Creating a Basic Observable

Let’s create a basic observable first and then see what they offer us.

Check out the code below for an example:


import { Component, OnInit } from '@angular/core';
import {Observable} from 'rxjs/Observable'
@Component({
  selector: 'app-observable-demo',
  templateUrl: './observable-demo.component.html',
  styleUrls: ['./observable-demo.component.css']
})
export class ObservableDemoComponent implements OnInit
 {
  private data: Observable;
  private fruits: Array = [];
  private anyErrors: boolean;
  private finished: boolean;
  constructor() { }
ngOnInit(){
}
Start(){
  this.data = new Observable
  (
    observer =>
    {
            setTimeout(() =>
            {
                observer.next('Apple');
            }, 1000);
           
            setTimeout(() =>
            {
                observer.next('mango');
            }, 2000);
            setTimeout(() =>
            {
                observer.next('Orannge');
            }, 3000);
            setTimeout(() =>
            {
                observer.complete();
            }, 4000);
           
   }
);
let subscription = this.data. subscribe(
fruit => this.fruits.push(fruit),
    error => this.anyErrors = false,
    () => this.finished = true
);
this.; }}

Let’s go through this code, step-by-step:

1. Import the observable from rxjs/Observable and include it in the component.

2. Next, create the observable object, which, in our example, we are creating the observable object which will be a string.

3. Next, subscribe to the Observable in the application which will allow us to listen to the data that is coming along with it.

4. With the subscription, we used three callbacks which already accept the data emitted by the observable. The second callback given is the Error call.

To Invoke this observable, we need to make some changes in the template that we are using. The code I used for the template looks like this:

<p >Observable Basics</p>
<hr/>
<b>Observable Data </b>
<div *ngFor="let f of fruits"> {{ f | uppercase }}</div>
<hr>
<div * >
<b>Error Status :</b>
 {{anyErrors=? 'error occured ' : 'It All Good'}}
<hr>
</div>
<div > <b> completion status : </b> {{ finished= ? 'Observer completed ': '' }}</div>
<hr>
<button (click)="start()">Start Emitting</button>

 

Read About: Regular Expressions – (Regex) – Regular Expression

The above code displays the fruit names that we are emitting in the application. The output of the code can be seen below:

Angular Observables

 

The post Angular Observables Tutorial appeared first on .

]]>
173